c# - Filtering one IQueryable List with another -
i have 2 lists below:
var processedorders = this._requestreviewrecordservice.getall().tolist(); var orders = _orderrepository.table.where(o => o.orderstatusid == (int)orderstatus.complete && o.createdonutc < entityfunctions.addminutes(datetime.now, -minutes)).tolist();
the lists of different types, both contain property called orderid.
essentially want filter second list "orders" of records matching orderid.
i've tried linq's except method, seems play nicely primitive types.
can point me in right direction - didnt think quite challenging!
thanks in advance al
here sample you're after:
public class typeone { public int orderid { get; set; } public string someotherfield { get; set; } } public class typetwo { public int orderid { get; set; } public string mainfield { get; set; } } class program { static void main(string[] args) { // little bit of setup var first = new list<typeone>() { new typeone { orderid = 1, someotherfield = "one" }, new typeone { orderid = 2, someotherfield = "two" } } ; var second = new list<typetwo>() { new typetwo { orderid = 1, mainfield = "one" }, new typetwo { orderid = 2, mainfield = "two" }, new typetwo { orderid = 3, mainfield = "buckle" }, new typetwo { orderid = 4, mainfield = "myshoe" } }; // here's interesting bit var firstids = id in first select id.orderid; var query = item in second firstids.contains(item.orderid) select item; // , boring results foreach (var in query) { console.writeline(string.format("[orderid: {0}, mainfield: {1}]", i.orderid, i.mainfield)); } console.readline(); }
keep in mind example uses simple lists stores objects. approach doesn't work if you're using entitiy framework entityset source collection in of queries, because haven't implemented extension method contains<>() works ef. being said, materialise queries in lists first, , approach work.
Comments
Post a Comment