0

When writing a method for an oData service I have the below linq, for which I need to have a dynamic "where" clause to filter the results (the "new"s in the joins are for composite PKs in the Entity Data Model):

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;

My Linq is not very good and I'm trying to use the DynamicQueryable class to generate a "where" clause at runtime (it's built from various variables):

var returnData = query.Where(whereClause);

Since the "where" clause filters on values in the Attribute and AttributeItem entities it invariably contains things like

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

which will fail at runtime since "No property or field 'Attribute' exists in type 'ProductListing'".

I have tried to build an anonymous type in the "select" which contains all elements of ProductListing entity and those from Attribute and AttributeItem which I require to filter by, but I need a strongly typed entity of type "ProductListing" to return from the method call.

Can ANYONE please help?? Should I be using dynamic Joins instead of dynamic Wheres? Is there a way of Where-ing against entities that you're not Selecting? Should I be selecting an anonymous type /"let" and building a strongly typed entity afterwards?

Please, any help is massively appreciated.

rposbo

4

1 回答 1

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 子句,然后从结果中仅选择具体类型。

于 2010-09-28T11:12:00.860 回答