有没有办法要求.ToLookup
LINQ 提供的功能需要多个键?
我承认一开始这似乎不直观,我希望没有实际的方法可以做到这一点,但我希望有人知道一种方法。
我基本上希望能够通过两个值进行查找,例如 astring
和 an int
,并使用这两个值检索对象。
例子
public class MyClass {
public string StringProp {get;set;}
public int IntProp {get;set;}
public object MoreData {get;set;}
}
public class Main {
public void Main() {
HashSet<MyClass> set = new HashSet<MyClass>();
set.Add(new MyClass {StringProp = "a", IntProp = 1, MoreData = null});
set.Add(new MyClass {StringProp = "c", IntProp = 4, MoreData = new object()});
set.Add(new MyClass {StringProp = "a", IntProp = 2, MoreData = "upupdowndown"});
set.Add(new MyClass {StringProp = "c", IntProp = 1, MoreData = string.Empty});
set.Add(new MyClass {StringProp = "c", IntProp = 4, MoreData = string.Empty});
// Using 'var' because I don't know how this would be defined.
// I recognize that this will not compile - but this is what I'm trying to do.
var lookup = set.ToLookup(x => x.StringProp && x.IntProp)
MyClass c = lookup["a", 1].First(); // Should return the first element
IEnumerable<MyClass> list = lookup["c", 4]; // Should return the 2nd and last elements
}
}