Search
×
Tuesday, November 12, 2019

# Code Smithery

Blog articles about writing code; be it C#, JavaScript, HTML or CSS. If its about coding we'll do it here!

Ibrahim Malluf

# Using Linq to sort a list on the sum of multiple fields

## Not rocket science, but I had to put in my 2 cents on this StackOverflow question.

A programmer on Stack Overflow ask a question about sorting a list based upon the sum of multiple fields. Someone else answered the question before I could and gave the simplest approach and a very good answer. But I wanted to get my answer in too, so I posted this response to Stack Overflow.
First the Question:

I have another LINQ calculation question.
I have a list made of class items:
```List<StudyAreasClass> Production = new List<StudyAreasClass>();
Production.Add(new StudyAreasClass() { Plant = "Plant A", Value1 = 94.4, Value2 = 97.2, Value3 = 71.9, Value4 = 12.8 });
Production.Add(new StudyAreasClass() { Plant = "Plant B", Value1 = 84.1, Value2 = 95.2, Value3 = 64.8, Value4 = 92.5 });
Production.Add(new StudyAreasClass() { Plant = "Plant C", Value1 = 43.1, Value2 = 66.3, Value3 = 92.7, Value4 = 84.0 });
Production.Add(new StudyAreasClass() { Plant = "Plant D", Value1 = 72.6, Value2 = 51.2, Value3 = 87.9, Value4 = 68.1 });
```
I would like to reorder that list but based on the sum of Value1,2,3 or Value1,2,3,4 for each entry in the list.
So I would like to keep the list in its current form with all the separate values so I can iterate through it, however I would like it to be in the calculated order.
How could I achieve this?

Well Amy, another poster gave this answer:
```List<ProductionClass> orderedProduction = Production
.OrderBy(saClass => saClass.Value1 + saClass.Value2 + saClass.Value3 + saClass.Value4)
.ToList();
```

That's actually a good answer and the story could end here happily!
But for me, the story didn't end here. To be fair, I did not see Amy's answer until after I posted mine. My take on it had a view a little bit wider than the specific request. My thoughts where that if the Sums of the values of the underlying class where used here to sort the list, might this sum of values have use elsewhere as well? My response was to wrap the sum of values in an extension class that added this functionality without modifying the underlying model. Here is the code I posted:
.
```namespace ConsoleApp1
{
/// <summary>
/// A class that adds an extension to the underlying StudyAreasClass
/// that provides the required summary of values.
/// </summary>
public static class GetSum
{
public static double SumOfValues(this StudyAreasClass item)
{
return item.Value1 + item.Value2 + item.Value3 + item.Value4;
}
}

public class StudyAreasClass
{
public string Plant { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
public double Value4 { get; set; }

}

class Program
{

static void Main(string[] args)
{

List<StudyAreasClass> Production = new List<StudyAreasClass>();
Production.Add(new StudyAreasClass() { Plant = "Plant A", Value1 = 94.4, Value2 = 97.2, Value3 = 71.9, Value4 = 12.8 });
Production.Add(new StudyAreasClass() { Plant = "Plant B", Value1 = 84.1, Value2 = 95.2, Value3 = 64.8, Value4 = 92.5 });
Production.Add(new StudyAreasClass() { Plant = "Plant C", Value1 = 43.1, Value2 = 66.3, Value3 = 92.7, Value4 = 84.0 });
Production.Add(new StudyAreasClass() { Plant = "Plant D", Value1 = 72.6, Value2 = 51.2, Value3 = 87.9, Value4 = 68.1 });

List<StudyAreasClass> orderedProduction = Production.OrderBy(row => row.SumOfValues()).ToList<StudyAreasClass>();
foreach(StudyAreasClass item in orderedProduction)
{
Console.WriteLine(\$" {item.Plant} {item.SumOfValues()}");
}

}

}
}
```

In the end, I really wanted to show that when considering a solution to a particular problem, we should also consider how this solution could affect other code and try to derive the most benefit as much as possible.
Next Article When DataTable Parameters really come in handy
Print