LINQ-列表到字典,按多个属性分组
我有一个包含2000项的哈希集,如下所示。索引不是该项的属性:LINQ-列表到字典,按多个属性分组,linq,grouping,Linq,Grouping,我有一个包含2000项的哈希集,如下所示。索引不是该项的属性: Index LocationId UserId OtherProperty 1 1 1 abc 2 1 2 zyx 3 1 3 nvme 4 1 4
Index LocationId UserId OtherProperty
1 1 1 abc
2 1 2 zyx
3 1 3 nvme
4 1 4 pcie
5 2 1 test
6 2 2 temp
7 2 3 etc
8 2 4 hah
...........................................
2000 500 4 last
如果我这样做:
hashSet.GroupBy(o => o.LocationId)
.ToDictionary(g => g.Key, g => g.ToList());
我得到了一本有500个位置的字典,每个位置包含4个用户的列表
但是我需要一个包含2000个条目的字典,其中保留了其他属性,每个属性都有一个包含4个用户的列表。我该怎么做?你的问题不清楚。我疯狂地猜测了你在尝试什么,并为你举了一些例子:
static void Main(string[] args)
{
List<MyClass> list = new List<MyClass>()
{
new MyClass() { Index = 1, LocationId = 1, UserId = 1, OtherProperty = "abc" },
new MyClass() { Index = 2, LocationId = 1, UserId = 2, OtherProperty = "qwe" },
new MyClass() { Index = 3, LocationId = 1, UserId = 3, OtherProperty = "asd" },
new MyClass() { Index = 4, LocationId = 1, UserId = 1, OtherProperty = "yxc" },
new MyClass() { Index = 5, LocationId = 2, UserId = 2, OtherProperty = "acb" },
new MyClass() { Index = 6, LocationId = 2, UserId = 3, OtherProperty = "ghj" },
new MyClass() { Index = 7, LocationId = 2, UserId = 1, OtherProperty = "uio" },
new MyClass() { Index = 8, LocationId = 2, UserId = 2, OtherProperty = "zhn" }
};
// Index is Unique => We build a Dictionary with one object per index.
Dictionary<int, MyClass> dicIndexToObject = list.ToDictionary(d => d.Index);
// We got multiple objects per Locations => We need a Lookup
ILookup<int, MyClass> lookupLocationToObject = list.ToLookup(l => l.LocationId);
// If we ask with a key, we get a List:
IEnumerable<MyClass> tmp = lookupLocationToObject[1];
Console.WriteLine("--- What do we have per LocationId? ---");
foreach (MyClass obj1 in tmp)
{
Console.WriteLine(obj1.ToString());
}
ILookup<int, MyClass> lookupUserIdToObject = list.ToLookup(l => l.UserId);
Console.WriteLine("--- What do we have per UserId? ---");
foreach (MyClass obj2 in lookupUserIdToObject[1])
{
Console.WriteLine(obj2.ToString());
}
// What if you want to get deeper?
// What if we want to search for an index within a Location?
Dictionary<int, Dictionary<int, MyClass>> dicLocationToIndexToObject =
list.GroupBy(l => l.LocationId) // We group by location
.ToDictionary(g => g.Key, values => values.ToDictionary(k => k.Index)); // We form the grouping to a dictionary. Instead of accepting a List as value, we form another dictionary
// if we now want to search for a index in a specific location we do this:
Console.WriteLine("--- Let's get out object for Index '1' in location '1' ---");
Dictionary<int, MyClass> tmp2 = dicLocationToIndexToObject[1];
MyClass obj = tmp2[1];
Console.WriteLine(obj);
Console.WriteLine("--- Lets get all objects for UserId '1' in location '2' ---");
Dictionary<int, ILookup<int, MyClass>> dicLocationToUserIdLookup =
list.GroupBy(l => l.LocationId) // We group by location
.ToDictionary(g => g.Key, values => values.ToLookup(k => k.UserId));
foreach (MyClass obj3 in dicLocationToUserIdLookup[2][1])
{
Console.WriteLine(obj3.ToString());
}
}
public class MyClass
{
public int Index { get; set; }
public int LocationId { get; set; }
public int UserId { get; set; }
public string OtherProperty { get; set; }
public override string ToString()
{
return String.Format(" Index: {0}, LocationId: {1}, UserId: {2}, OtherProperty: {3}", this.Index, this.LocationId, this.UserId, this.OtherProperty);
}
}
显示您希望从示例数据中获得的输出。您希望字典中的值是什么类型的对象?希望字典键有什么?你不能有重复的键,这是字典的重点。如果你想要2000个项目,不要在LocationId上分组。不完全理解,但是如果你需要索引,你可以做一些类似hashSet.SelectValue,index=>new{Value,index}.GroupByo=>o.index,o=>o.Value的事情。但这并不是使用字典的真正理由。