Search code examples
c#sqllinqsql-to-linq-conversion

What is the exact LINQ Query for this SQL Query?


i am very new in LINQ so,I can't understand what the exact Linq query is of this SQLquery.

Please give exact Linq Query similar to this SQL query.

Select  * From tblProduct 
Where ProductId In 
      (Select  ProductId  from  tblViewer Where ViewerId = 123)

Solution

  • Contains is the most straightforwrad way to implement an IN statement, so it would be something like:

    tblProduct.Where(p => tblViewer.Where(v => v.ViewerId == 123)
                                   .Select(v => v.ProductId)
                                   .Contains(p.ProductId)
                    );