是否可以创建一些 IGrouping 对象 [英] Is it possible to create some IGrouping object
问题描述
我有 List
.
是否可以在此列表中添加新项目?或者实际上,是否可以创建一些 IGrouping 对象?
如果你真的想创建自己的IGrouping
,实现一个简单的接口:
public class Grouping:List, IGrouping{公共分组(TKey 密钥):base() =>键 = 键;公共分组(TKey key,int容量):base(容量)=>键 = 键;公共分组(TKey 键,IEnumerable 集合):基础(集合)=>键 = 键;公共 TKey 密钥 { 获取;}}
<块引用>
注意:您不应该尝试允许 Key
可设置,主要是因为该键应由包含此 Grouping 的集合管理.
该类继承自List
并实现了IGrouping
接口.除了成为 IEnumerable
和 IEnumerable
(List
满足)的要求之外,要实现的唯一属性是 键
.
您可以从头开始创建这些组的List
:
var groups = new List>();groups.Add(new Grouping("a", new string [] { "apple" }));groups.Add(new Grouping("p", new string [] { "peach", "pear" }));groups.Add(new Grouping("o", new string [] { "orange" }));//内联变体:组=新列表<分组<字符串,字符串>>{new Grouping("a", new string[] { "apple" }),new Grouping("p", new string[] { "peach", "pear" }),new Grouping("o", new string[] { "orange" }),};
或者您可以使用此结构将新组附加到已评估为列表的先前 Linq GroupBy
表达式的结果:
var strings = new string [] { "apple", "peach", "pear";};var groups = strings.GroupBy(x => x.First().ToString()).ToList();…//向列表中注入一个新项,无需重新查询groups.Add(new Grouping("o", new string [] { "orange" }));
如果您需要将项目添加到从 IGrouping 表达式解析的组中,您可以将 Linq 结果转换为 Grouping
的 List
:
var strings = new string [] { "apple", "peach", "orange";};var groupExpression = strings.GroupBy(x => x.First().ToString());var editableGroups = groupExpression.Select(x => new Grouping(x.Key, x)).ToList();…//添加梨"到p"列表,并检查该组是否首先退出.var pGroup = editableGroups.FirstOrDefault(x => x.Key == "p");如果(pGroup == null)editableGroups.Add(new Grouping("p", new string[] { "pear" }));别的pGroup.Add(梨");
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?
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");
这篇关于是否可以创建一些 IGrouping 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!