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();
2

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Agreed. You gotta make a trade off here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.