Using linq to sort an object collection?
-
Hello, I'm new to LINQ but I have a collection of values stored in an object to determine how much money a sales person is bringing in(called "salesTransactions"). The data looks like the following: Employee ID | Employee Name | Sales Transaction Amount --------------------------------------------------- 1 | John | 18.47 2 | Beth | 27.67 1 | John | 12.04 1 | John | 10.50 3 | Allen | 34.87 2 | Beth | 19.12 How can I use LINQ to take the object and display the total earnings sorted by the total amount earned, like the following: Beth: $46.79 John: $41.01 Allen: $34.87 Is this possible with using LINQ? Thanks.
-
Hello, I'm new to LINQ but I have a collection of values stored in an object to determine how much money a sales person is bringing in(called "salesTransactions"). The data looks like the following: Employee ID | Employee Name | Sales Transaction Amount --------------------------------------------------- 1 | John | 18.47 2 | Beth | 27.67 1 | John | 12.04 1 | John | 10.50 3 | Allen | 34.87 2 | Beth | 19.12 How can I use LINQ to take the object and display the total earnings sorted by the total amount earned, like the following: Beth: $46.79 John: $41.01 Allen: $34.87 Is this possible with using LINQ? Thanks.
-
Hello, I'm new to LINQ but I have a collection of values stored in an object to determine how much money a sales person is bringing in(called "salesTransactions"). The data looks like the following: Employee ID | Employee Name | Sales Transaction Amount --------------------------------------------------- 1 | John | 18.47 2 | Beth | 27.67 1 | John | 12.04 1 | John | 10.50 3 | Allen | 34.87 2 | Beth | 19.12 How can I use LINQ to take the object and display the total earnings sorted by the total amount earned, like the following: Beth: $46.79 John: $41.01 Allen: $34.87 Is this possible with using LINQ? Thanks.
Something like this? var salesTransaction = from s in salesTransactions group s by s.EmployeeID into g select new {EmployeeID = g.Key, TotalSalesTransactionAmount = g.Group.Sum(s => s.SalesTransactionAmount)}; NOT TESTED!
Andreas Johansson
IT Professional at Office IT Partner i Norrbotten Sweden
What we don't know. We learn.
What you don't know. We teach -
Something like this? var salesTransaction = from s in salesTransactions group s by s.EmployeeID into g select new {EmployeeID = g.Key, TotalSalesTransactionAmount = g.Group.Sum(s => s.SalesTransactionAmount)}; NOT TESTED!
Andreas Johansson
IT Professional at Office IT Partner i Norrbotten Sweden
What we don't know. We learn.
What you don't know. We teachHi. Thanks for the help so far everyone. I tried your code & I think I'm close but the data coming back isn't grouping nor summing: List list = Sale.Sales; //gets all my data var salesTransaction = from s in list group s by s.EmployeeID into g select new { EmployeeID = g.Key, TotalUserPointsEarned = g.Sum(s => s.SalesTransactionAmount) }; A few notes: -My vs.net 2008 didn't like the "g.Group.Sum". After googling this, it seems microsoft removed the "Group" and it's now just "g.Sum". -In my original message, my sample employee id's were simply 1,2, & 3. In reality, they're GUID's. Not sure if this makes a difference or not. So currently, the results I get back are exactly what I started out with: 9c79640f-4ff4-4a53-94f9-eb75ba2d4e63 | John | 18.47 803eedde-886a-4e6b-9e38-9b17d42e3d28 | Beth | 27.67 9c79640f-4ff4-4a53-94f9-eb75ba2d4e63 | John | 12.04 9c79640f-4ff4-4a53-94f9-eb75ba2d4e63 | John | 10.50 660yyokk-443b-3k0n-2a99-6b10u41h4x88 | Allen | 34.87 803eedde-886a-4e6b-9e38-9b17d42e3d28 | Beth | 19.12 Any idea what I did wrong? Thanks. -Goalie35
-
Hi. Thanks for the help so far everyone. I tried your code & I think I'm close but the data coming back isn't grouping nor summing: List list = Sale.Sales; //gets all my data var salesTransaction = from s in list group s by s.EmployeeID into g select new { EmployeeID = g.Key, TotalUserPointsEarned = g.Sum(s => s.SalesTransactionAmount) }; A few notes: -My vs.net 2008 didn't like the "g.Group.Sum". After googling this, it seems microsoft removed the "Group" and it's now just "g.Sum". -In my original message, my sample employee id's were simply 1,2, & 3. In reality, they're GUID's. Not sure if this makes a difference or not. So currently, the results I get back are exactly what I started out with: 9c79640f-4ff4-4a53-94f9-eb75ba2d4e63 | John | 18.47 803eedde-886a-4e6b-9e38-9b17d42e3d28 | Beth | 27.67 9c79640f-4ff4-4a53-94f9-eb75ba2d4e63 | John | 12.04 9c79640f-4ff4-4a53-94f9-eb75ba2d4e63 | John | 10.50 660yyokk-443b-3k0n-2a99-6b10u41h4x88 | Allen | 34.87 803eedde-886a-4e6b-9e38-9b17d42e3d28 | Beth | 19.12 Any idea what I did wrong? Thanks. -Goalie35
Hi! Are you sure that the result comes from the new enumerable salesTransaction? Because according to the select, each object in that collection should only have two properties, EmployeeID and TotalPointsEarned. The result looks like the list "list"
Andreas Johansson
IT Professional at Office IT Partner i Norrbotten Sweden
What we don't know. We learn.
What you don't know. We teach