2010-09-28 74 views
0

爲oData服務編寫方法時我有以下linq,爲此我需要一個動態的「where」子句來過濾結果(以下簡稱「新」 S的連接是在實體數據模型複合PKS):實體數據模型,動態Linq,多表動態Where子句,強類型返回類型

var query = from pl in CurrentDataSource.ProductListing 
      join pla in CurrentDataSource.ProductListingAttribute 
       on new {pl.ProductID, pl.WebCategoryID, pl.ParentProductID} 
       equals new {pla.ProductID, pla.WebCategoryID, pla.ParentProductID} 
      join att in CurrentDataSource.Attribute 
       on pla.AttributeID 
       equals att.AttributeID 
      join attItem in CurrentDataSource.AttributeItem 
       on pla.AttributeItemID 
       equals attItem.AttributeItemID 
      select pl; 

我的LINQ的不是很好,我嘗試使用DynamicQueryable類來生成一個「where」子句在運行時(它由各種變量構成):

var returnData = query.Where(whereClause); 

由於「where」子句過濾器上的屬性值和AttributeItem實體它總是包含像

"((Attribute.Value='foo' AND AttributeItem.Value='bar') 
OR 
(Attribute.Value='sna' AND AttributeItem.Value='fu'))" 

的事情,因爲「沒有屬性或字段‘屬性’型‘ProductListing’存在」,這將在運行時失敗。

我曾試圖建立一個匿名類型「選擇」,其中包含與ProductListing實體的所有元素那些屬性和AttributeItem,我需要進行過濾,但我需要類型的強類型實體「ProductListing」來從方法調用返回。

任何人都可以請幫忙嗎?我應該使用動態連接而不是動態Wheres?有沒有辦法針對你不選擇的實體?我應該選擇一個匿名類型/「讓」,然後建立一個強類型的實體嗎?

請大力讚賞任何幫助。

rposbo

回答

0

解決我特別的查詢是要做到這一點:

  var query = from pl in CurrentDataSource.ProductListing 
          join pla in CurrentDataSource.ProductListingAttribute 
           on new {pl.ProductID, pl.WebCategoryID, pl.ParentProductID} 
           equals new {pla.ProductID, pla.WebCategoryID, pla.ParentProductID} 
          join att in CurrentDataSource.Attribute 
           on pla.AttributeID 
           equals att.AttributeID 
          join attItem in CurrentDataSource.AttributeItem 
           on pla.AttributeItemID 
           equals attItem.AttributeItemID 
         select new 
         { 
          ProductListing = pl, 
          att.AttributeName, 
          attItem.AttributeValue 
         }; 

      var returnData = query.Where(whereClause).Select(o => o.ProductListing); 

即選擇包含具體類型匿名類型,應用where子句到,然後只選擇具體類型從結果。