What would be the equivalent of SQL [IN] statement for C# Lambda Expression?

c#, lambda

Solution

Following code should work:

var someInvoiceList = new int[] {1, 2, 3};
var result = list_A.Where(x => someInvoiceList.Contains(x.InvoiceID));

Problem

I am in this scenario. I have two lists of two different object types, which both have a shared property value. Let's say that it is `invoiceID`. In SQL, if I want to grab all records from `table_A` given that it has column `invoiceID` value match with any `invoiceId` inside `table_B`, I would probably do something like this. ``` Select * From table_A where invoiceID in ( select invoiceId from table_B) ``` What would be the equivalent LINQ or Lambda expression in C# for this scenario? I am used to search for elements in the list using a single value such as: ``` var result = list_A.Where(x=>x.InvoiceID = someInvoiceID) ``` or `.contains()` instead. However, this could cover only a single `invoiceID` value. I guess, I could run the loop for each `invoiceID` on `list_B` and then store the result in another list, but I'm just wondering if there is a better way to do this?

Original source

Related problems