c# - comparing two lists in linq -


i have 2 lists: orders , items , need compare them name , if order list have bool set false , if satysfy condition copies string message , adds new list , have no freaking idea how compare lists, appreciated function:

 private static void faileditemslist(list<item> failed, list<order> orders, list<item> items) {     foreach(var order in orders)     {         foreach(var item in items)         {             if(order.equals(item))             {                 if(order.fulfilled == false)                 {                     item.failmessage = order.failmessage;                     failed.add(item);                 }             }         }     } } 

in general return "fail-list" method instead of passing it. use linq simplify task:

static list<item> getfaileditems(list<order> orders, list<item> items) {     var failed = order in orders                  !order.fulfilled                  join item in items on order.name equals item.name                  select new { order, item};      list<item> faileditems = new list<item>();     foreach (var x in failed)     {         x.item.failmessage = x.order.failmessage;         faileditems.add(x.item);     }      return faileditems; } 

since item , order different classes have removed equals check , replaced join on name property desired. since linq should not cause side effects haven't modifed item.failmessage property in query in foreach-loop. due linq's deferred execution(the query gets executed @ foreach) not less efficient.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -