4

I have a list of data structures that have a property called group. Now, I would like to split this list in multiple lists per group-value. For example, I have 5 objects in the original list with the following group-values:

0. group: "group1"
1. group: "group1"
2. group: "group2"
3. group: "group2"
4. group: "group3"

Keeping that list in mind I would like 3 lists coming out of this per group-value. One list will only have objects with group "group1", the next one "group2" and so on...

I guess I can do this with Linq (I can do this to get a single list from a group with a given value) but I can't figure out how to do it automatically with all possible groups.

Dries
  • 995
  • 2
  • 16
  • 45
  • http://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq is a modified version of this scenario where one groups 3 members each so with only the group by and select modified to meet your situation it should be the same question/answer as it should be for your situation. In case that wouldn't work / doesn't work in your case please modify your question to include also how it is different from the situation there. – Thomas Nov 10 '15 at 12:24
  • 1
    `Enumerable.GroupBy` is the command you are looking for https://msdn.microsoft.com/en-us/library/vstudio/bb534501(v=vs.100).aspx – Chris Nov 10 '15 at 12:27
  • What is your output datastructure? Or do you just want it as an anonymous type. – blogbydev Nov 10 '15 at 12:27

3 Answers3

4

GroupBy does what you are looking for. But if you want the results in a list containing one list per group, you should use it like this:

IList<List<Item>> groups = items.GroupBy(x => x.Group).Select(x => x.ToList()).ToList();
Diego
  • 16,436
  • 26
  • 84
  • 136
3

Assuming you have multiple types in the same collection, I think you should introduce an interface (name is up for debate ;))

public interface IHaveGroup 
{
    string Group { get; }
}

Then, you can just use LINQ.

IEnumerable<IHaveGroup> items = // get all items as a single group
var groups = items.GroupBy(x => x.Group);
mstaessen
  • 1,227
  • 11
  • 16
  • 2
    why do you need the interface? – Jens Nov 10 '15 at 12:27
  • He says he has multiple data structures, so I assumed there would also be multiple datatypes in the same collection – mstaessen Nov 10 '15 at 12:28
  • He still is grouping by just 1 single field (and its the same always). And the data structures I thought of as different properties of the same class. maybe something for the OP to eloberate. – Thomas Nov 10 '15 at 12:29
  • The GroupBy thing was exactly what I was looking for! Thanks a lot! Since you were first with your answer I'll accept it when it's available. Also to clarify: I'm just using one type of datastructure in this list – Dries Nov 10 '15 at 12:32
0

You can use group be feature of Linq. I'm adding a sample, the output will be.

  • Group group1 has 2 elements
  • Group group2 has 2 elements
  • Group group3 has 1 elements

    List<GroupTemp> list = new List<GroupTemp>();
    list.Add(new GroupTemp("group1", "1"));
    list.Add(new GroupTemp("group1", "2"));
    list.Add(new GroupTemp("group2", "3"));
    list.Add(new GroupTemp("group2", "4"));
    list.Add(new GroupTemp("group3", "5"));
    
    var groupedList = list.GroupBy(t => t.key).ToList();
    
    foreach (var entity in groupedList)
    {
        Console.WriteLine(String.Format("Group {0} has {1} elements", entity.Key, entity.Count()));
    }
    
Ugur Basak
  • 317
  • 4
  • 9