c# - How to get last duplicate item from a list -


i have list contains duplicate items, , need last entry of items.

what tried below, can't find further solution achieve need to.

     list<listitem> listitems = new list<listitem>         {             new listitem{ item1="item1", item2="item2"},             new listitem{ item1="item2", item2="item22"},             new listitem{ item1="item3", item2="item23"},             new listitem{ item1="item4", item2="item24"},             new listitem{ item1="item4", item2="item244"},             new listitem{ item1="item4", item2="item244"},             new listitem{ item1="item5", item2="item25"},             new listitem{ item1="item1", item2="item12"},             new listitem{ item1="item6", item2="item26"},             new listitem{ item1="item7", item2="item27"},             new listitem{ item1="item1", item2="item22"},             new listitem{ item1="item8", item2="item28"},             new listitem{ item1="item9", item2="item29"},             new listitem{ item1="item8", item2="item288"},             new listitem{ item1="item10", item2="item210"},             new listitem{ item1="item8", item2="item288"},             new listitem{ item1="item11", item2="item211"},         };          listitems.groupby(x => x.item1).tolist().foreach(r =>                 {                     //want last item w.r.t. "r". because have complex scenario if not works have second  last record, don't want lose groupby list.                     foreach (var item in r)                  {                         console.writeline(item.item1 + " " + item.item2);                  }                 }); 

expected output:

item1 item22 item2 item22 item3 item23 item4 item244 item5 item25 item6 item26 item7 item27 item8 item288 item9 item29 item10 item210 item11 item211 

fiddle : https://dotnetfiddle.net/tmacie

    var result = listitems                 .groupby(x=>new {x.item1,x.item2})                 .select(g=>new {item1 = g.key.item1,item2 = g.max(m=>m.item2)}); 

if criteria duplicates item1

    var result = listitems                 .groupby(x=>x.item1)                 .select(g=>new {item1 = g.key,item2 = g.max(m=>m.item2)}); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -