实体数据模型、动态Linq、多表动态Where 子句、强类型返回类型
在为 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’类型中不存在属性或字段‘Attribute’”。
我尝试在“select”中构建一个匿名类型,其中包含 ProductListing 实体的所有元素以及来自我需要过滤的 Attribute 和 AttributeItem 的元素,但我需要一个“ProductListing”类型的强类型实体从该方法返回称呼。
有人可以帮忙吗?我应该使用动态连接而不是动态Wheres 吗?有没有一种方法可以针对您未选择的实体进行定位?我应该选择匿名类型/“let”并随后构建强类型实体吗?
请您提供任何帮助,我们将不胜感激。
罗波斯博
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的特定查询的解决方案是这样做:
即选择包含具体类型的匿名类型,对其应用 where 子句,然后仅从结果中选择具体类型。
The solution to my particular query was to do this:
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.