Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Using linq to sort an object collection?

Using linq to sort an object collection?

Scheduled Pinned Locked Moved LINQ
questioncsharplinqsales
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Goalie35
    wrote on last edited by
    #1

    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.

    S A 2 Replies Last reply
    0
    • G Goalie35

      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.

      S Offline
      S Offline
      SayreCC
      wrote on last edited by
      #2

      Hi, can you post your code?

      Hope this one can help. Thanks Hi, Please select Good Question if my answer are fit to your Question.

      1 Reply Last reply
      0
      • G Goalie35

        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.

        A Offline
        A Offline
        Andreas X
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • A Andreas X

          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

          G Offline
          G Offline
          Goalie35
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • G 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

            A Offline
            A Offline
            Andreas X
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups