C# 这对Linq有可能吗?

C# 这对Linq有可能吗?,c#,asp.net,linq,entity-framework,C#,Asp.net,Linq,Entity Framework,下面我试图传入一个int列表,以将productID==的所有产品返回给每个int public IQueryable<Product> GetProductsForSubCat(List<int> prodSubResult) { if (prodSubResult != null) { var _db = new ProductContext();

下面我试图传入一个int列表,以将productID==的所有产品返回给每个int

 public IQueryable<Product> GetProductsForSubCat(List<int> prodSubResult)
        {

            if (prodSubResult != null)
            {
                var _db = new ProductContext();

                IQueryable<Product> query = _db.Products;

                foreach (int x in prodSubResult)
                {
                     query = _db.Products.Where(p => p.ProductID == x);



                }
                    return query;
            }
            return null;
        }
public IQueryable GetProductsForSubCat(列出prodSubResult)
{
if(prodSubResult!=null)
{
var_db=new ProductContext();
IQueryable查询=_db.Products;
foreach(prodSubResult中的int x)
{
query=_db.Products.Where(p=>p.ProductID==x);
}
返回查询;
}
返回null;
}

是的,你可以,而且非常简单:

var query = _db.Products.Where(p => prodSubResult.Contains(p.ProductId));

你为什么要这样做?如果列表有多个元素,它应该总是返回一个空集合。您现在应该知道,使用LINQ可以做任何事情。这是正确的,前提是OP没有寻找要返回的
IEnumerable
。可能不会,但这还不够清楚,可能会发生。@Bobson是的,我的回答只是基于OP想要什么的描述,而不是提供的实现(其中有其他缺陷)。这样做有什么缺陷?@jackncoke我不确定你说的缺陷是什么。这本质上等同于SQL
,其中ProductId位于(…)
<代码>查询包含所有
产品
,其
产品ID
在提供的ID列表中。很抱歉,可能我没有阅读您的评论。谢谢你的帮助!