繁体   English   中英

实体数据模型,动态Linq,多表动态Where子句,强类型返回类型

[英]Entity Data Model, Dynamic Linq, multiple table dynamic Where clause, strongly typed return types

在为oData服务编写方法时,我具有下面的linq,为此我需要有一个动态的“ where”子句以过滤结果(联接中的“ new”是针对实体数据模型中的复合PK)的:

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”子句会过滤Attribute和AttributeItem实体中的值,因此它始终包含诸如

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

由于“类型'ProductListing'中不存在属性或字段'属性'”,这将在运行时失败。

我试图在“选择”中构建一个匿名类型,该类型包含ProductListing实体的所有元素以及需要过滤的Attribute和AttributeItem中的元素,但是我需要一个类型为“ ProductListing”的强类型实体才能从方法中返回呼叫。

任何人都可以帮忙吗? 我应该使用动态联接而不是动态Wheres吗? 有没有一种方法可以针对未选择的实体? 我是否应该选择一个匿名类型/“ let”,然后再构建一个强类型实体?

请提供任何帮助,我们将不胜感激。

rposbo

我的特定查询的解决方案是这样做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM