1

I have 2 lists Variant and MaterialOrder and in list variant, I have more than 500+ materials and in list MaterialOrder, I have 100 materials. Here I need to sort the materials in variant based on Material order.

public class Variant { public virtual string ItemNo { get; set; } } public class MaterialOrder { public string ItemNo { get; set; } public int Orderno { get; set; } } var MaterialOrderdetails = (await materialTask)?.ToList() ?? new List<MaterialOrder>(); // List all materials with Orderno // variants holds 500+ materials // getting error At least one object must implement IComparable. variants = variants.OrderBy(a => MaterialOrderdetails.Where(b => b.ItemNo == a.ItemNo)); 
2
  • Does this answer your question? List sort based on another list Commented Dec 21, 2023 at 22:17
  • "Here I need to sort the materials in variant based on MaterialOrder" Ok, so you need to sort the objects in sequence variant. But what order do you want? Do you want them in the same order as the ItemNo in sequence of MaterialOrders? What if you have an ItemNo in variant that is not in MaterialOrders? Consider to edit the question and write a proper unambiguous specification Commented Dec 24, 2023 at 16:10

2 Answers 2

0

You need to have the a => return something orderable. Presumably you want the OrderNo of the matching materialOrder?

I'd strongly recommend linking the data structures somehow, or creating a dictionary or a submethod for this (even a generic submethod), this usage of linq crosses the line from making your code streamlined into making it unmaintainable but...

var variants = new List<Variant>(); var materialOrders = new List<MaterialOrder>(); variants = variants.OrderBy(a => materialOrders.Where(b => b.ItemNo == a.ItemNo) .FirstOrDefault()?.Orderno ?? 99999999999999).ToList(); 

your linq query needs to - get a match from the materialOrders list, grab the first match, then return the order number to be used by the orderBy query.

The ?? 9999 argument is so that where there are no matches, you won't get a null exception. You could do something like the following instead if you do actually want it to fail, but have it fail gracefully

var variants = new List<Variant>(); var materialOrders = new List<MaterialOrder>(); variants = variants.OrderBy(a => materialOrders.Where(b => b.ItemNo == a.ItemNo) .FirstOrDefault()?.Orderno ?? throw new Exception($"varient {a.ItemNo} has no match in the orders table")) .ToList(); 
Sign up to request clarification or add additional context in comments.

3 Comments

: Throws error Sequence contains no elements
@user2432361 my bad, .First throws exceptions on no elements. FirstOrDefault should have been used (returns null) - just tested and it works fine
I can confirm you'd only get that error when a variant has no order though
0

If you want to stick to LINQ, while still maintaning performance, you can use the join operator to pair each Variant with its MaterialOrder.

variants = from variant in variants join order in MaterialOrderdetails on variant.ItemNo equals order.ItemNo orderby order.Orderno select variant; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.