GroupBy then do aggregation on results for each group - c#

I am confused by the groupby behavior in LINQ to objects. Lets assume for this I have the following class;
public class person
{
public string name { get; set; }
public int age { get; set; }
}
Lets say I have a list or type person; List<person> people
Now, I want to produce an IEnumerable<T> with an IGrouping or anonymous type that has two properties 1) the name (the key) and 2) the sum of all of the ages for people with that name.
Here are a couple of examples of things I've tried (unsuccessfully);
people.GroupBy(x => x.name, x => x, (key, value) => value.Aggregate((c, n) => c + n));
That won't compile with the error cannot convert type "int" to "namespace.person"
Before that I was trying something more along the lines of;
people.GroupBy(x => x.name).Select(g => new { g.Key, g.Aggregate((c, n) => c + n)) } );
Which essentially gives the same error. I'm basically struggling to understand what the value returned by GroupBy really is. At first I thought that basic overload was giving me a key value pair where x.Key was the key I specified with my delegate and x.Value would be an IEnumerable<T> where the typeof T would be the type of x. Of course, if that were the case my second example would work like a charm. My question is somewhat open ended but can someone explain 2 things, firstly; How do I accomplish my end goal with LINQ? And Secondly, why isn't the result of GroupBy more along the lines of what I describe here? What is it? I feel like a key value pair where the value is a collection of objects that match that key is far more intuitive than what is actually returned.

var grouped = people.GroupBy(x => x.name)
.Select(x => new
{
Name = x.Key,
Age = x.Sum(v => v.age),
Result = g.Aggregate(new Int32(), (current, next) => next.age + next.age)
});
If you want you can group the result of that again by Name and it will be a grouping with Key as the name and Age as the value

you can do it with expression syntax
var results = from p in persons
group p.car by p.name into g
select new { name = g.Key, age = g.Sum(c=>.age };

Related

IGrouping <decimal,string> does not contain a defintion for 'name'

Using LINQ and lambda expressions, I am trying to write data that I have pulled to a text file.
using (var contextDb = new TimoToolEntities())
{
using (var writeFile = new StreamWriter(saveTo))
{
var randomData = contextDb.WorkCenter_Operations.Where(d => d.Job_Number >= 1 && d.Part_Number.Length >= 1 && d.Oper_Number >= 1 )
.OrderBy(d => d.Oper_Number)
.GroupBy(d => d.Job_Number , d => d.Part_Number ).ToList();
foreach (var record in randomData)
{
Console.WriteLine(record.Job_Number + "," + record.Part_Number); // error here
}
}
Console.ReadLine();
}
I am getting the error the 'IGrouping does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type 'IGrouping' could be found.
I have looked around and believe that the objects are anonymous, but I haven't been able to find a fix that will work.
When you use this overload of GroupBy
.GroupBy(d => d.Job_Number , d => d.Part_Number )
the first lambda is a key selector (you group by Job_Number) and the second one is a value selector. Your record will be a collection of Part_Number with Job_Number as a key.
This MSDN example illustrates the basic usage:
// Group the pets using Age as the key value
// and selecting only the pet's Name for each value.
IEnumerable<IGrouping<int, string>> query =
pets.GroupBy(pet => pet.Age, pet => pet.Name);
// Iterate over each IGrouping in the collection.
foreach (IGrouping<int, string> petGroup in query)
{
// Print the key value of the IGrouping.
Console.WriteLine(petGroup.Key);
// Iterate over each value in the
// IGrouping and print the value.
foreach (string name in petGroup)
Console.WriteLine(" {0}", name);
}
Your intent is not 100% clear, so just in case you actually wanted to group by multiple fields, use a different overload like this:
.GroupBy(d => new { d.Job_Number, d.Part_Number })
Then your record will be a collection of whatever your data is and will have an anonymous key where you can access for example record.Key.Job_Number

C# sort list in alphabetical order

How can I sort a list of users by last name in the formats below?
first.last
first.mi.last
mylist.Sort();
sorts by first name, and that is not what i want to do. Do I somehow need to use a RegEx?
You can use Linq to order your users:
using System.Linq;
mylist.OrderBy(x => x.LastName);
If you have same last names you can order users by middle name. If you have same middle names you can order users by first name:
mylist.OrderBy(x => x.LastName).ThenBy(x => x.MiddleName).ThenBy(x => x.FirstName);
Assuming that your names are simple strings (and not objects with FirstName and LastName properties) like this:
var list = new List<string> { "c.a", "a.c" , "b"};
you can order them like this:
var orderedList = list.OrderBy(item => item.Split('.').Last());
Output:
c.a
b
a.c
If you want to sort in place, try specifing the comparer
mylist.Sort((left, right) => string.Compare(left.LastName, right.LastName));
In case mylist contains string items, you have to extract LastName:
private static String LastName(string value) {
if (string.IsNullOrEmpty(value))
return value;
int p = value.LastIndexOf('.');
return value.SubString(p + 1);
}
...
mylist.Sort((left, right) => string.Compare(LastName(left), LastName(right)));

Convert a list to a dictionary and sum up values using linq

I have list i.e. List<Field>. This Field class contains a code and a value properties among other fields and I would like to be able to use linq in order to sum up all the values for the same code.
I know I could loop through my list and add this to a dictionary using the following code, but I'm sure there has to be a cleaner way to do this:
if (totals.ContainsKey(code))
{
totals[code] += value;
}
else
{
totals.Add(code, value);
}
Any ideas?
I found something similar, but this applied to a list> which isn't what I have:
var result = Sales.SelectMany(d => d) // Flatten the list of dictionaries
.GroupBy(kvp => kvp.Key, kvp => kvp.Value) // Group the products
.ToDictionary(g => g.Key, g => g.Sum());
from this article [Sum amount using Linq in <List<Dictionary<string, int>>]Sum amount using Linq in <List<Dictionary<string, int>>
Any ideas? I could always change my code to have a Dictionary<string, Field> but I'm sure there has to be a way to do this with a list and linq.
Thanks.
I have list i.e. List<Field>. This Field class contains a code and a value properties among other fields and I would like to be able to use linq in order to sum up all the values for the same code.
I know I could loop through my list and add this to a dictionary using the following code, but I'm sure there has to be a cleaner way to do this:
if (totals.ContainsKey(code))
{
totals[code] += value;
}
else
{
totals.Add(code, value);
}
Any ideas?
I found something similar, but this applied to a list> which isn't what I have:
var result = Sales.SelectMany(d => d) // Flatten the list of dictionaries
.GroupBy(kvp => kvp.Key, kvp => kvp.Value) // Group the products
.ToDictionary(g => g.Key, g => g.Sum());
from this article [Sum amount using Linq in <List<Dictionary<string, int>>]Sum amount using Linq in <List<Dictionary<string, int>>
Any ideas? I could always change my code to have a Dictionary<string, Field> but I'm sure there has to be a way to do this with a list and linq.
Thanks.
UPDATE:
Sorry, I think I omitted an important section in regards to the above. The list is contained within another list i.e. List> myitemList; which will contain other irrelevant fields which may require further filtering. I'm not sure???
NOTE: Sorry formatting is messed up once again!
To give a bit of context to this:
Item1 (of type List)
Item Name Value
(Item 1) Type 1
(Item 2) Description Test
(Item 3) Code A
(Item 4) Net 100.00
Item2 (of type List)
Item Name Value
(Item 1) Type 2
(Item 2) Description Test1
(Item 3) Code B
(Item 4) Net 95.55
Item3 (of type List)
Item Name Value
(Item 1) Type 2
(Item 2) Description Test2
(Item 3) Code A
(Item 4) Net 35.95
As you can see each list of type List contains 4 Field entries where my Field is defined with Name (String) and Value (Object)
Each of these list is then added to a main list. So I need to loop through the main list and in turn I want to end up with a dictionary what will contain the "Code" and sum of "Net" for each list. So at the end, I should just end up with
A, 135.95
B, 95.55
I don't know if the above make sense. I hope it does!
UPDATE
The fact that I'm dealing with a list> actually didn't make a different as I actually wanted to sum up one list at the time, so provide answer is correct! Thank you!
The code that you posted is almost what you need - for using it on the list you need to simplify it slightly:
var result = myList // No flattening
.GroupBy(x => x.Code) // Group the items by the Code
.ToDictionary(g => g.Key, g => g.Sum(v => v.Value)); // Total up the values
var list = new List<Field>
{
new Field { Code = "A", Value = 10 },
new Field { Code = "A", Value = 20 },
new Field { Code = "B", Value = 30 },
};
var dic = list
.GroupBy(z => z.Code)
.ToDictionary(z => z.Key, z => z.Sum(f => f.Value));
You can do:
Dictionary<string, int> dictionary =
list.GroupBy(r => r.ID)
.ToDictionary(grp => grp.Key, grp => grp.Sum(r => r.Value));
Considering you have class like:
public class MyClass
{
public string ID { get; set; }
public int Value { get; set; }
}

C# Sorting list by another list

I have now 2 lists:
list<string> names;
list<int> numbers;
and I need to sort my names based on the values in numbers.
I've been searching, and most use something like x.ID, but i don't really know what that value is. So that didn't work.
Does anyone know, what to do, or can help me out in the ID part?
So i assume that the elements in both lists are related through the index.
names.Select((n, index) => new { Name = n, Index = index })
.OrderBy(x => numbers.ElementAtOrDefault(x.Index))
.Select(x => x.Name)
.ToList();
But i would use another collection type like Dictionary<int,string> instead if both lists are related insomuch.
Maybe this is a task for the Zip method. Something like
names.Zip(numbers, (name, number) => new { name, number, })
will "zip" the two sequences into one. From there you can either order the sequence immediately, like
.OrderBy(a => a.number)
or you can instead create a Dictionary<,>, like
.ToDictionary(a => a.number, a => a.name)
But it sounds like what you really want is a SortedDictionary<,>, not a Dictionary<,> which is organized by hash codes. There's no LINQ method for creating a sorted dictionary, but just say
var sorted = new SortedDictionary<int, string>();
foreach (var a in zipResultSequence)
sorted.Add(a.number, a.name);
Or alternatively, with a SortedDictionary<,>, skip Linq entirely, an go like:
var sorted = new SortedDictionary<int, string>();
for (int idx = 0; idx < numbers.Count; ++idx) // supposing the two list have same Count
sorted.Add(numbers[idx], names[idx]);
To complement Tims answer, you can also use a custom data structure to associate one name with a number.
public class Person
{
public int Number { get; set; } // in this case you could also name it ID
public string Name { get; set; }
}
Then you would have a List<Person> persons; and you can sort this List by whatever Attribute you like:
List<Person> persons = new List<Person>();
persons.Add(new Person(){Number = 10, Name = "John Doe"});
persons.Add(new Person(){Number = 3, Name = "Max Muster"});
// sort by number
persons = persons.OrderBy(p=>p.Number).ToList();
// alternative sorting method
persons.Sort((a,b) => a.Number-b.Number);
I fixed it by doing it with an dictionary, this was the result:
dictionary.OrderBy(kv => kv.Value).Reverse().Select(kv => kv.Key).ToList();

Average extension method in Linq for default value

Anyone know how I can set a default value for an average? I have a line like this...
dbPlugins = (from p in dbPlugins
select new { Plugin = p, AvgScore = p.DbVersions.Average(x => x.DbRatings.Average(y => y.Score)) })
.OrderByDescending(x => x.AvgScore)
.Select(x => x.Plugin).ToList();
which throws an error becase I have no ratings yet. If I have none I want the average to default to 0. I was thinking this should be an extension method where I could specify what the default value should be.
There is: DefaultIfEmpty.
I 'm not sure about what your DbVersions and DbRatings are and which collection exactly has zero items, but this is the idea:
var emptyCollection = new List<int>();
var average = emptyCollection.DefaultIfEmpty(0).Average();
Update: (repeating what's said in the comments below to increase visibility)
If you find yourself needing to use DefaultIfEmpty on a collection of class type, remember that you can change the LINQ query to project before aggregating. For example:
class Item
{
public int Value { get; set; }
}
var list = new List<Item>();
var avg = list.Average(item => item.Value);
If you don't want to/can not construct a default Item with Value equal to 0, you can project to a collection of ints first and then supply a default:
var avg = list.Select(item => item.Value).DefaultIfEmpty(0).Average();
My advice would to create a reusable solution instead of a solution for this problem only.
Make an extension method AverageOrDefault, similar to FirstOrDefault. See extension methods demystified
public static class MyEnumerableExtensions
{
public static double AverageOrDefault(this IEnumerable<int> source)
{
// TODO: decide what to do if source equals null: exception or return default?
if (source.Any())
return source.Average();
else
return default(int);
}
}
There are 9 overloads of Enumerable.Average, so you'll need to create an AverageOrDefault for double, int?, decimal, etc. They all look similar.
Usage:
// Get the average order total or default per customer
var averageOrderTotalPerCustomer = myDbContext.Customers
.GroupJoin(myDbContext.Orders,
customer => customer.Id,
order => order.CustomerId,
(customer, ordersOfThisCustomer) => new
{
Id = customer.Id,
Name = customer.Name,
AverageOrder = ordersOfThisCustomer.AverageOrDefault(),
});
I don't think there's a way to select default, but how about this query
dbPlugins = (from p in dbPlugins
select new {
Plugin = p, AvgScore =
p.DbVersions.Any(x => x.DbRatings) ?
p.DbVersions.Average(x => x.DbRatings.Average(y => y.Score)) : 0 })
.OrderByDescending(x => x.AvgScore)
.Select(x => x.Plugin).ToList();
Essentially the same as yours, but we first ask if there are any ratings before averaging them. If not, we return 0.

Categories

Resources