C # Linq - GroupedEnumerable usages?

 Fruit[] data = new[] {
                new Fruit {Name = "Gala",Type = "Apple",Price = 0.75m,Quantity = 10}, 
                new Fruit {Name = "Granny Smith",Type = "Apple",Price = 0.80m,Quantity = 7},
                new Fruit {Name = "Tasty",Type = "Strawberries",Price = 1.90m,Quantity = 20}
                       };

 var grouped = from fruit in data
                          group fruit by fruit.Type;

grouped A type: System.Linq.GroupedEnumerable<ConsoleApplication15.Fruit,string,ConsoleApplication15.Fruit>

look at him: enter image description here

my question is:

If grouped contains elements (implements ienumerable) - why its type GroupedEnumerable

and not

IEnumerable <System.Linq.IGrouping<string,ConsoleApplication15.Fruit>>

why did they create a new aka type GroupedEnumerable?

+3
source share
1 answer

If you have an object in .Net, it must be of a certain type, it cannot be an interface. So, for example, it is new IList<int>()invalid, you should do something like new List<int>().

If you have a variable, the type can be an interface, so you can write, for example IList<int> list = new List<int>().

, . grouped IEnumerable<IGrouping<string,Fruit>> ( , var). , , - GroupedEnumerable<Fruit, string, Fruit>, , GroupBy(). IEnumerable, .

Visual Studio , : .

+7

All Articles