1

So. I've got a list Property that groups values with identical names;

List<IGrouping<string, MyItemDTO>> MyList { get; set; }

It's initially populated from a database by means of an IQueryable<>.

But say I wanted to add a new item to this list using code, how would I go about that? Since IGrouping is an interface, I can't exactly new it up like;

IGrouping<string, MyItemDTO> newGroup =  new IGrouping<string, MyItemDTO>();
MyList.Add(newGroup);

That doesn't work. I'd like to know, 'what does?' How do I get a new item in there?

Many thanks.

EDIT:

A little perspective:

Imagine having two or more cities. These cities have a few respective streets with matching names. IGrouping<,> ensures that the street name appears only once in the List. Therefore the data-bound Combobox doesn't repeat any values (typically because the end user won't know to what city the street belongs to).

My view has two Comboboxes. One for street, the other for city. Should a street be selected, the viewmodel will select the corresponding city, unless the street group contains more that one street within it. At that point, the user will be prompted to manually select which city the street resides in.

I figured that IGrouping<,> would suit this purpose seeing as it's the default return type when using the GroupBy() extension method. Still open to suggestions though.

Agnel Amodia
  • 765
  • 8
  • 18
Offer
  • 630
  • 2
  • 11
  • 28
  • 5
    You don't. `IGrouping` is the thing returned by `GroupBy`. It is explicitly not designed to be constructable outside the provider. If you insisted you could of course implement your own `IGrouping` -- but a much saner approach is to convert the `IGrouping`s to something else instead, like a `Dictionary>`. – Jeroen Mostert Mar 20 '18 at 12:53
  • 3
    You should probably explain your *original* problem and why you though `IGrouping<,>` would be the solution to it. – Panagiotis Kanavos Mar 20 '18 at 12:55
  • You can use generics to create a new IGrouping instance go to [this solution](https://stackoverflow.com/a/5073144/7296188) – mehmetgelmedi Mar 20 '18 at 13:40
  • @PanagiotisKanavos I've updated my description giving a little back story to what led me to do it this way. Perhaps you could have another look? – Offer Mar 20 '18 at 13:52

3 Answers3

2

Use this method to create IGrouping value:

IGrouping<string, MyItemDTO> GetIGrouping(string key,MyItemDTO item)
{
    return new[] {item}.GroupBy(val => key).First();
}

or if you want several items in group:

IGrouping<string, MyItemDTO> GetIGrouping(string key,IEnumerable<MyItemDTO> items)
{
    return items.GroupBy(val => key).First();
}
Evgeny Gorbovoy
  • 765
  • 3
  • 20
  • Note that the result of this, when simply added to a list, can be incorrect if the intent is for the list to contain *unique* groupings (as would normally be returned by `GroupBy` methods). This needs to be taken into account when writing code that operates on the list. – Jeroen Mostert Mar 20 '18 at 13:09
0

Either use a GroupBy call on an IEnumerable<T> that produces what you want, or implement IGrouping<TKey, TElement> as per this answer to a question about that particular approach.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
0

I think You really do not needed IGrouping in your Property , What rather you need is a collection like Dictionary , List etc.

Still if you want it then here's how you should add items with IGrouping

var t = new List<Test>();
foreach (var item in t.GroupBy(x => x.A))
{
  A.MyList.Add(item);
}

public static class A
{
    public static List<IGrouping<string, Test>> MyList { get; set; }
}

 public class Test
    {
        public string A { get; set; }
        public int B { get; set; }
    }

Hope that helps !!

Vishal Sharma
  • 2,773
  • 2
  • 24
  • 36
  • I've updated the description to shed light on the scenario. Out of curiosity, how would a dictionary be better suited? – Offer Mar 20 '18 at 13:56