Reputation: 71
I m trying to group a list using c# program on Xamarin .
List EList;
Sample Data is
ExpList(Item, dop,Value)
grocery 01/03/2019 1000
clothes 04/03/2019 250
grocery 01/04/2019 500
movie 02/03/2018 550
clothes 02/05/2019 550
I want to group the list by Item Name and need to calculate its total Value . ie.
Group the list
grocery 01/03/2019 1000
grocery 01/04/2019 500
clothes 04/03/2019 250
clothes 02/05/2019 550
movie 02/03/2018 550
Calculate
I want some information on group like below
Total group Items is 3
grocery Number of times 2 and Value is 1500
clothes Number of times 2 and Value is 800
movie Number of times 1 and Value is 550
Upvotes: 0
Views: 100
Reputation: 18861
Refer the following code
public class People
{
public string Name { get; set; }
public string Date { get; set; }
public double Value { get; set; }
}
List<People> peopleList = new List<People>()
{
new People { Name="grocery ",Date="01/03/2019",Value=1000 },
new People { Name="clothes ",Date="02/03/2019",Value=250 },
new People { Name="grocery ",Date="04/03/2019",Value=500 },
new People { Name="clothes ",Date="01/02/2019",Value=550 },
new People { Name="movie ",Date="03/03/2019",Value=550 },
};
var groupList = peopleList.GroupBy(x => x.Name).Select(x => new { name = x.Key,Total=x.Sum(y=>y.Value),Num=x.Distinct().Count() }).Distinct().ToList();
Console.WriteLine("Total group Items is "+groupList.Count() );
foreach(var item in groupList)
{
Console.WriteLine(item.name + " Number of times "+ item.Num + " and Value is " + item.Total);
}
Upvotes: 1