47

I have List<IGrouping<string,string>>.

Is is somehow possible to add new item to this list? Or actually, is it possible to create some IGrouping object?

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • 3
    The answers below show you how you could do this, but you probably want to considering implementing a `Dictionary`, `Lookup` or other structure more suited to your purpose. .NET has plenty of great classes to help with structuring your data and I'm confident that something already exists that will meet your needs. – Kirk Broadhurst Feb 22 '11 at 03:32
  • As this caused a lot of questions why I do this, here it is: I queried to get result and stored it, and I knew that source has changed and I believed that it was less costly to add new element manually than to query again. – Ivan Ičin Feb 22 '11 at 14:28

7 Answers7

74

If you really wanted to create your own IGrouping<TKey, TElement>, it is a simple interface to implement:

public class Grouping<TKey, TElement> : List<TElement>, IGrouping<TKey, TElement>
{
    public Grouping(TKey key) : base() => Key = key;
    public Grouping(TKey key, int capacity) : base(capacity) => Key = key;
    public Grouping(TKey key, IEnumerable<TElement> collection)
        : base(collection) => Key = key;
    public TKey Key { get; }
}

Note: you shouldn't try to allow the Key to be settable, mainly because the key should be managed by the collection that this Grouping is contained within.

This class inherits from List<T> and implements the IGrouping interface. Aside of the requirement of being an IEnumerable and IEnumerable<TElement> (which List<T> satisfies) the only property to implement is Key.

You could create List of these groups from scratch:

var groups = new List<Grouping<string, string>>();
groups.Add(new Grouping<string,string>("a", new string [] { "apple" }));
groups.Add(new Grouping<string,string>("p", new string [] { "peach", "pear" }));
groups.Add(new Grouping<string,string>("o", new string [] { "orange" }));

// inline variant:
groups = new List<Grouping<string, string>>
{
    new Grouping<string, string>("a", new string[] { "apple" }),
    new Grouping<string, string>("p", new string[] { "peach", "pear" }),
    new Grouping<string, string>("o", new string[] { "orange" }),
};

Or you could use this structure to append new groups to the results of a previous Linq GroupBy expression that has been evaluated into a list:

var strings = new string [] { "apple", "peach", "pear" };
var groups = strings.GroupBy(x => x.First().ToString()).ToList();
…
// Inject a new item to the list, without having to re-query
groups.Add(new Grouping<string,string>("o", new string [] { "orange" }));

If you need to add Items to the groups resolved from an IGrouping expression you can cast the Linq results into a List of Grouping:

var strings = new string [] { "apple", "peach", "orange" };
var groupExpression = strings.GroupBy(x => x.First().ToString());
var editableGroups = groupExpression.Select(x => new Grouping<string,string>(x.Key, x)).ToList();
…
// Add "pear" to the "p" list, with a check that the group exits first.
var pGroup = editableGroups.FirstOrDefault(x => x.Key == "p");
if (pGroup == null)
    editableGroups.Add(new Grouping<string, string>("p", new string[] { "pear" }));
else
    pGroup.Add("pear");
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
Nathan Anderson
  • 6,768
  • 26
  • 29
  • 1
    So when I instantiate this, I can obviosly set the key, but how do I set the items? – Neil N Sep 17 '12 at 16:42
  • 1
    Since `MyGrouping` inherits from `List`, it has all the properties and methods of that class. See it's documentation here: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx – Nathan Anderson Sep 18 '12 at 16:29
  • Sorry I meant to post a follow up comment. I used AddRange() to dump in an existing list. – Neil N Sep 18 '12 at 17:21
  • 7
    Probably the easiest approach would be something like: var newGrouping = new List().GroupBy(t => t.MyKey); – aBetterGamer Sep 18 '14 at 19:03
  • Enhanced version: ' public class Grouping : List, IGrouping { public Grouping(TKey key) : base() => Key = key; public Grouping(int capacity, TKey key) : base(capacity) => Key = key; public Grouping(IEnumerable collection, TKey key) : base(collection) => Key = key; public TKey Key { get; private set; } }' – Cosmin Sontu Aug 27 '19 at 16:21
  • Not all groups of data can easily or efficiently be formed from a single Linq statement, this solution allows us to build grouping manually, but still use the results in CollectionViewSource for Xaml collection controls. – Chris Schaller Apr 09 '20 at 05:04
15

As of .NET 4.0, there do not appear to be any public types in the BCL that implement the IGrouping<TKey, TElement> interface, so you won't be able to 'new one up' with any ease.

Of course, there's nothing stopping you from:

  1. Creating a concrete type yourself that implements the interface, as @Nathan Anderson points out.
  2. Getting an instance / instances of IGrouping<TKey, TElement> from a LINQ query such as ToLookup and GroupBy and adding it / them to your list.
  3. Calling ToList() on an existing sequence of groups (from ToLookup / GroupBy).

Example:

IEnumerable<Foo> foos = ..
var barsByFoo = foos.ToLookup(foo => foo.GetBar());

var listOfGroups = new List<IGrouping<Foo, Bar>>();

listOfGroups.Add(barsByFoo.First()); // a single group
listOfGroups.AddRange(barsByFoo.Take(3)); // multiple groups

It's not clear why you would want to do this, though.

Ani
  • 111,048
  • 26
  • 262
  • 307
10
IGrouping<TKey, TElement> CreateGroup<TKey, TElement>(IEnumerable<TElement> theSeqenceToGroup, TKey valueForKey)
{
    return theSeqenceToGroup.GroupBy(stg => valueForKey).FirstOrDefault();
}
dovid
  • 6,354
  • 3
  • 33
  • 73
7

You can also hack the grouping by not grouping on something within the list:

var listOfGroups = new[] { "a1", "a2", "b1" }
                       .GroupBy(x => x.Substring(0, 1))
                       .ToList();

// baz is not anything to do with foo or bar yet we group on it
var newGroup = new[] { "foo", "bar" }.GroupBy(x => "baz").Single();

listOfGroups.Add(newGroup);

listOfGroups then contains:

a:
    a1, a2
b:
    b1
baz:
    foo, bar

IDEOne example

dav_i
  • 27,509
  • 17
  • 104
  • 136
3

Based on dovid's answer, I created the following extension method, that creates an instance of IGrouping<TKey, TElement> from an IEnumerable<TElement> and a key of type TKey:

public static IGrouping<TKey, TElement> ToGroup<TKey, TElement>(
    this IEnumerable<TElement> elements,
    TKey keyValue)
{
    return elements.GroupBy(_ => keyValue).FirstOrDefault();
}

It can be called like this:

var fruits = new [] { "Apples", "Bananas" };
var myFruitsGroup = fruits.ToGroup("fruitsKey");

Beware that ToGroup() can return null.

I also created an additional GroupBy extension method:

public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Func<TSource, IEnumerable<TElement>> elementsSelector)
{
    return source
        .Select(s => elementsSelector(s)
            .ToGroup(keySelector(s)))
        .Where(g => g != default(IGrouping<TKey, TElement>));
}

It can be used like this:

var foodItems = new []
{
    new { Category = "Fruits", Items = new [] { "Apples", "Bananas" } },
    new { Category = "Vegetables", Items = new [] { "Tomatoes", "Broccoli" } },
};
var categoryGroups = foodItems.GroupBy(i => i.Category, i => i.Items);
lauxjpn
  • 4,749
  • 1
  • 20
  • 40
1
var headers = from header in new[] {
  new { Name = "One", List = new[] { "One 1", "One 2", "One 2" } },
  new { Name = "Two", List = new[] { "Two 1", "Two 2", "Two 2" } }
} from value in header.List group value by header.Name;
Mega WEB
  • 69
  • 1
  • 4
-4

The IGrouping interface is for the GroupBy() operator in LINQ. You would normally get an IGrouping object from a LINQ query with a group by clause. It doesn't make much sense to have a list of groupings, though.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • 15
    Well, given that the return type of GroupBy is `IEnumerable>` is actually makes a lot of sense to have a list of groupings, considering that List is just one of the many ways to have an `IEnumerable`. – Anthony Nov 07 '12 at 22:00