Where list contains list in list

I am just posting this data to the Reporting Engine (SSRS) in Asp.net MVC5.
It's ok, but this query is taking a long time since I have to go through ListProducts

(I guess ListProducts

is the size of the database matches).

I'm just looking for a way to optimize this query.

I've tried any

and contains

(as shown below) but they don't seem to work on the same table.

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

      

I got this from here

2) I also tried

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

      

but that also doesn't work and generates a compile time error which

System.Guid does not contain a definition of "Contains"

Is there another way, or am I doing this the only correct way?

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; }
}

      

+3


source to share


1 answer


Ok can you do:



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

      

0


source







All Articles