Multiple table dynamic, Entity Data Model, Dynamic Linq Strongly typed return types, when clause

anonymous-types c# dynamic-linq linq-to-entities

Question

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

1
0
9/28/2010 8:48:39 AM

Popular Answer

The solution to my particular query was to do this:

            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);

i.e., select an anonymous type containing a concrete type, apply the where clause to that then select only the concrete type from the result.

0
9/28/2010 11:12:00 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow