Question

I have a TList<SKU> that I want to sort by the SKU object id's. Below is how the list is obtained from the database (base code from the third party vendor being used).

TList<SKU> skuList = skuAdmin.GetByProductID(this.itemId); *the itemId is an integer value representing the product id.

I want to sort this list based on the the SKUID attribute that every SKU has. I've been looking at the sorts & orders for List<> but I can't seem to find a solution that I can tailor to my need.

This: Sorting TList<object> (ntiers) with ThenBy was probably the closest I could find to what I want to do.

What I want to do is return the SKU list item with the lowest the SKUID can anyone help me out?

Was it helpful?

Solution

If you want to order the whole list.. you can do:

list = list.OrderBy(x => x.SKUID).ToList();

If you want the lowest one, you order them, then take the first one:

var first = list.OrderBy(x => x.SKUID).FirstOrDefault(); 
// will be null or the SKU with lowest SKUID.

OTHER TIPS

How about:

TList<SKU> skuList = skuAdmin.GetByProductID(this.itemId).OrderBy(item=> item.Id).ToList(); // Or OrderByDescending() but you said you want lowest to highest, so OrderBy should be used.

Hope that helps.

Assuming that the SKUID's are unique then something like this should return the lowest one or null if the list is empty:

SKU lowestSKU = skuList.Find(x => x.SKUID == skuList.Min<SKU>(y => y.SKUID));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top