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. linq group by and custom aggregate

linq group by and custom aggregate

Scheduled Pinned Locked Moved LINQ
csharplinqquestion
4 Posts 2 Posters 3 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.
  • L Offline
    L Offline
    Leblanc Meneses 0
    wrote on last edited by
    #1

    trying to create a group by string concat. here is what i have: public String Concat(IEnumerable source, String delimiter) { String s = String.Empty; foreach (String item in source) { s += item + delimiter; } return (s == String.Empty) ? String.Empty : s.Substring(0, s.Length - delimiter.Length); } public class CustomObject { public CustomObject() { } public String Group { get; set; } public String Value { get; set; } } List items = new List() { new CustomObject(){Group = "type1", Value="1"}, new CustomObject(){Group = "type1", Value="2"}, new CustomObject(){Group = "type1", Value="3"}, new CustomObject(){Group = "type1", Value="4"}, new CustomObject(){Group = "type1", Value="5"}, new CustomObject(){Group = "type2", Value="10"}, new CustomObject(){Group = "type2", Value="20"}, new CustomObject(){Group = "type2", Value="30"}, new CustomObject(){Group = "type2", Value="40"}, new CustomObject(){Group = "type2", Value="50"}, }; var grouped = from item in items orderby item.Group, item.Value group item by item.Group into grp select new { Category = grp.Key, DataValue = grp }; output results should be: type1 | 1,2,3,4,5 type2 | 10,20,30,40,50 anyone want to share how i do this.. thanks

    J 1 Reply Last reply
    0
    • L Leblanc Meneses 0

      trying to create a group by string concat. here is what i have: public String Concat(IEnumerable source, String delimiter) { String s = String.Empty; foreach (String item in source) { s += item + delimiter; } return (s == String.Empty) ? String.Empty : s.Substring(0, s.Length - delimiter.Length); } public class CustomObject { public CustomObject() { } public String Group { get; set; } public String Value { get; set; } } List items = new List() { new CustomObject(){Group = "type1", Value="1"}, new CustomObject(){Group = "type1", Value="2"}, new CustomObject(){Group = "type1", Value="3"}, new CustomObject(){Group = "type1", Value="4"}, new CustomObject(){Group = "type1", Value="5"}, new CustomObject(){Group = "type2", Value="10"}, new CustomObject(){Group = "type2", Value="20"}, new CustomObject(){Group = "type2", Value="30"}, new CustomObject(){Group = "type2", Value="40"}, new CustomObject(){Group = "type2", Value="50"}, }; var grouped = from item in items orderby item.Group, item.Value group item by item.Group into grp select new { Category = grp.Key, DataValue = grp }; output results should be: type1 | 1,2,3,4,5 type2 | 10,20,30,40,50 anyone want to share how i do this.. thanks

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      The only problem I see with your code is that "grp" is an enumerable list of CustomObject, not an enumerable list of strings Therefore changing the following lines:

      Leblanc Meneses wrote:

      String s = String.Empty; foreach (String item in source) { s += item + delimiter; }

      to

      String s = String.Empty;
      foreach (CustomObject item in source)
      {
      s += item.Value + delimiter;
      }

      give the output your expecting. Finally, remember not to use string concatenation using the + operator in real code - it is horribly slow and inefficient. Look at StringBuilder in the System.Text namespace.

      L 1 Reply Last reply
      0
      • J J4amieC

        The only problem I see with your code is that "grp" is an enumerable list of CustomObject, not an enumerable list of strings Therefore changing the following lines:

        Leblanc Meneses wrote:

        String s = String.Empty; foreach (String item in source) { s += item + delimiter; }

        to

        String s = String.Empty;
        foreach (CustomObject item in source)
        {
        s += item.Value + delimiter;
        }

        give the output your expecting. Finally, remember not to use string concatenation using the + operator in real code - it is horribly slow and inefficient. Look at StringBuilder in the System.Text namespace.

        L Offline
        L Offline
        Leblanc Meneses 0
        wrote on last edited by
        #3

        that is a solution but i rather make the linq query create an IEnumerable of strings this way i can reuse the concat aggregate on other custom objects. would you know what the linq query should change to?

        L 1 Reply Last reply
        0
        • L Leblanc Meneses 0

          that is a solution but i rather make the linq query create an IEnumerable of strings this way i can reuse the concat aggregate on other custom objects. would you know what the linq query should change to?

          L Offline
          L Offline
          Leblanc Meneses 0
          wrote on last edited by
          #4

          By default, the elements in each grouping are untransformed input elements. I simply had to supply an element selector in the grouby. var grouped = from item in items orderby item.Group, item.Value group item.Value by item.Group into grp select new { Key = grp.Key, Concated = grp.Concat(",") };

          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