1

I have an array of an object called storing, some of the object's attribute (skuID) is the same with some of the other storing with different (storingID), how do i make my array distinct depending on their (skuID)?

Storing(string storingID, skuID, storageID, price, expiry)

I have tried this but it doesn't work:

List<storing> storingAll = (List<storing>)Session["storingAll"];
List<storing> displayedStoring = storingAll.Distinct().ToArray();
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
Kerzoz
  • 331
  • 5
  • 18

1 Answers1

6
storingAll.GroupBy(x=>x.skuID).Select(group=>group.First());

Note that this solution uses no external libraries. Simple group by the first occurrence of skuID.

Reference: How to get a distinct list from a List of objects?

Community
  • 1
  • 1
Sahir
  • 329
  • 1
  • 2
  • 9
  • 1
    It uses no additional libraries, but may well consume a lot more memory than using MoreLINQ, as it needs to build up the groups. It's also non-streaming: it has to read *all* the elements before it can emit *any* of them. – Jon Skeet May 08 '17 at 06:44
  • Agreed. You gotta make a trade off here. – Sahir May 08 '17 at 06:45