键值对搜索 [英] keyvalue pair searching
问题描述
大家好,
我想知道..如何简化搜索?
Hi Everyone,
I would like to know.. how to simplify this search?
list.Where(x => x.Key == "1" || x.Key == "6"|| x.Key == "7" || x.Key == "8" || x.Key == "9" || x.Key == "10" ||x.Key == "11").ToList();
我有一个从下面的枚举派生的值对列表.
I have a value pair list derived from this enum below.
public enum LegacyTo : int
{
Spouse = 1,
Lifepartner = 2,
Survivingspouse = 3,
Survivinglifepartner = 4,
Children = 5,
Parents = 6,
Parentsinlaw = 7,
ParentsandParentsinlaw = 8,
Other = 9,
Institution = 10,
InterVivostrust = 11
}
现在的情况是,我有条件要过滤:Survivingspouse = 3,ParentsandParentsinlaw = 8,InterVivostrust = 11 ...目前我正以这种方式进行操作,更像是意大利面条...
Now the case is, i have conditions where i have to filter for :Survivingspouse = 3,ParentsandParentsinlaw = 8,InterVivostrust = 11...currently im doing it this way and its more like spaghetti...
list.Where(x => x.Key == "1" || x.Key == "6"|| x.Key == "7" || x.Key == "8" || x.Key == "9" || x.Key == "10" ||x.Key == "11").ToList();
提前感谢...
Thanx in advance...
推荐答案
如果您可以更改枚举值,并且Key是枚举,则它变得很简单:
If you can change the enum values, and the Key is the enum, then it becomes simple:
[Flags]
public enum LegacyTo : int
{
Spouse = 0x0001,
Lifepartner = 0x0002,
Survivingspouse = 0x0004,
Survivinglifepartner = 0x0008,
Children = 0x0010,
Parents = 0x0020,
Parentsinlaw = 0x0040,
ParentsandParentsinlaw = 0x0080,
Other = 0x0100,
Institution = 0x0200,
InterVivostrust = 0x0400
}
然后您可以通过 Xoring 与组合值进行与"运算,对照多个值检查该值.如果存在任何一个,则结果将为非零.
[edit]
[Flags]属性已添加(感谢phil.o!)
XOR变成AND(感谢Manfred!)-因为我是个白痴. OriginalGriff
[/edit]
You can then check the value against multiple values by XORing ANDing with the combined value. If any one of them is present, the result will be non-zero.
[edit]
[Flags] attribute added (thanks phil.o!)
XOR becomes AND (thanks Manfred!)- because I''m an idiot. OriginalGriff
[/edit]
这篇关于键值对搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!