Linq的"IN"操作员

Eri*_*ick 13 c# linq entity-framework c#-3.0

我试图在Linq中使用Entity Framework转换旧的原始Sql查询.

它使用IN运算符和一组项目.查询是这样的:

SELECT Members.Name
FROM Members
WHERE Members.ID IN ( SELECT DISTINCT ManufacturerID FROM Products WHERE Active = 1)
ORDER BY Members.Name ASC
Run Code Online (Sandbox Code Playgroud)

由于子查询的返回不是单个字符串而是字符串集合,因此我无法使用该String.Contains()方法.

我想过做的事情:

var activeProducts = (
from products in db.ProductSet
where product.Active == true
select product.ManufacturerID);
Run Code Online (Sandbox Code Playgroud)

然后

var activeMembers = (
from member in db.ContactSet
where member.ID.ToString().Contains(activeProducts));
Run Code Online (Sandbox Code Playgroud)

但它停止在包含说它有无效的参数...我不能选择activeProducts.ManufacturerID,因为很明显,proprety不存在,因为它返回IQueryable ...

我在这里要做的就是返回一个至少有一个活跃产品的成员列表.

任何提示?

[编辑]

这是完整的查询代码......我尝试使用第二个表达式的包含,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();
Run Code Online (Sandbox Code Playgroud)

[更多细节] ManufacturerID显然是一个可以为空的GUID ......

由于某种原因,ContactSet类不包含对产品的任何引用,我想我将不得不进行连接查询,这里没有线索.

spa*_*ves 15

var activeMembers = (
from member in db.ContactSet
where activeProducts.Select(x=>x.ID).Contains(member.ID));
Run Code Online (Sandbox Code Playgroud)


SLa*_*aks 5

试试where activeProducts.Contains(member.ID).
编辑:你有没有尝试过ToString吗?