linq group by and custom aggregate
-
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
-
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
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.
-
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.
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?
-
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?
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(",") };