C# 使用KeyedList获取项目
我有一个用C#编写的小类,用来保存数据结构的列表,并使用一个特殊的键来分组项目C# 使用KeyedList获取项目,c#,c#-4.0,windows-phone-8,winrt-xaml,C#,C# 4.0,Windows Phone 8,Winrt Xaml,我有一个用C#编写的小类,用来保存数据结构的列表,并使用一个特殊的键来分组项目 public class KeyedList<TKey, TItem> : List<TItem> { public TKey Key { protected set; get; } public IEnumerable<TItem> Items { protected set; get; } public KeyedList(TKey key, IEnu
public class KeyedList<TKey, TItem> : List<TItem>
{
public TKey Key { protected set; get; }
public IEnumerable<TItem> Items { protected set; get; }
public KeyedList(TKey key, IEnumerable<TItem> items)
: base(items)
{
Key = key;
Items = items;
}
public KeyedList(IGrouping<TKey, TItem> grouping)
:base (grouping)
{
Key = grouping.Key;
???
}
}
公共类KeyedList:List
{
公钥{protected set;get;}
公共IEnumerable项{protected set;get;}
公钥列表(TKey、IEnumerable项)
:基本(项目)
{
钥匙=钥匙;
项目=项目;
}
公钥列表(I分组)
:基本(分组)
{
Key=grouping.Key;
???
}
}
现在我想访问元素
那么,我要写些什么呢???要获取项的信息?首先,您不应该存储项,因为它们已经由基类处理:
List
但是如果您真的想这样做,您可以直接将分组
分配到项
,因为i分组
实现了IEnumerable
:
类似于
Items=grouping.Select(g=>g)代码>?
public KeyedList(IGrouping<TKey, TItem> grouping)
:base (grouping)
{
Key = grouping.Key;
Items = grouping;
}
public KeyedList(IGrouping<TKey, TItem> grouping)
:base (grouping)
{
Key = grouping.Key;
Items = this;
}