【发布时间】:2010-01-14 17:22:33
【问题描述】:
我正在尝试使用实体框架在 Linq 中转换旧的原始 Sql 查询。
它使用了 IN 操作符和一个项目集合。查询是这样的:
SELECT Members.Name
FROM Members
WHERE Members.ID IN ( SELECT DISTINCT ManufacturerID FROM Products WHERE Active = 1)
ORDER BY Members.Name ASC
由于子查询返回的不是单个字符串而是字符串集合,所以我不能使用String.Contains() 方法。
我想过做类似的事情:
var activeProducts = (
from products in db.ProductSet
where product.Active == true
select product.ManufacturerID);
然后
var activeMembers = (
from member in db.ContactSet
where member.ID.ToString().Contains(activeProducts));
但它停在包含说它有无效参数...我不能选择 activeProducts.ManufacturerID 因为显然属性不存在,因为它返回一个 IQueryable...
我在这里要做的是返回至少拥有一种有效产品的成员列表。
有什么提示吗?
[编辑]
这是完整的查询代码...我尝试在第二个表达式中使用 contains,Linq 似乎不喜欢它:
Server Error in '/' Application.
LINQ to Entities does not recognize the method 'Boolean Contains[String](System.Linq.IQueryable``1[System.String], System.String)' method, and this method cannot be translated into a store expression.
var activeProduct =(from product in Master.DataContext.ProductSet
where product.Active == true
&& product.ShowOnWebSite == true
&& product.AvailableDate <= DateTime.Today
&& ( product.DiscontinuationDate == null || product.DiscontinuationDate >= DateTime.Today )
select product.ManufacturerID.ToString() );
var activeArtists = from artist in Master.DataContext.ContactSet
where activeProduct.Contains(artist.ID.ToString())
select artist;
NumberOfArtists = activeArtists.Count();
artistsRepeater.DataSource = activeArtists;
artistsRepeater.DataBind();
[更多详情] ManufacturerID 显然是一个可为空的 GUID...
由于某种原因,ContactSet 类不包含对产品的任何引用,我想我将不得不进行连接查询,这里没有线索。
【问题讨论】:
标签: c# linq entity-framework c#-3.0