Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - Is it possible to create some IGrouping object

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...