列表包含列表

时间:2015-06-12 12:07:15

标签: c# asp.net-mvc linq contains any

我只是将此数据发送到Asp.net MVC5中的报告引擎(SSRS) 一切都很好,但是这个查询需要花费很多时间,因为我必须遍历ListProducts(我猜ListProducts是数据库匹配的大小)。

我正在寻找一种优化此查询的方法。

我已尝试anycontains(如下所示),但它们似乎无法在单个表格中使用。

context.Products.Where(w => w.ProductDetail.Any(a => a.startDate >= startDate 
                                                     && a.endDate <= endDate))

我是从here

得到的

2)我尝试了this as well

context.Products.Where(w => ListProducts.Any(x =>w.Contains(x)))

但这也不起作用并产生编译时错误

  

System.Guid不包含&#39;包含&#39;

的定义

还有其他方法,或者我这样做是唯一正确的方法吗?

foreach (var item in ListProducts)
{
    List.AddRange(_context.Products.Where(w => w.ProductId== item).Select(q => new ProductVM
    {
        Name = q.Name,
        Quantity = q.Quantity,

    }).ToList().Select(item=> new ProductVM
    {
             Name = item.Name,
        Quantity = item.Quantity,
    }).ToList());
}


public class Product
{
    public Nullable<System.Guid> ProductId { get; set; }
    public string Name { get; set;}
    public decimal Quantity { get; set; }
}

1 个答案:

答案 0 :(得分:0)

好的,你可以这样做:

var thelist = _context.Products.Where(n => ListProducts.Contains(n.ProductId)).Select(n => new ProductVM
{
     Name = n.Name,
     Quantity = n.Quantity
}).ToList();