[英]How can I use LINQ to retrieve entries that contain more than one property of a list within a list?
I have a List of IJapaneseDictionaryEntry
objects which are described below. 我有一个
IJapaneseDictionaryEntry
对象的列表,如下所述。 Inside this are IKanji
objects that contain Priorites
objects. 这里面是
IKanji
包含对象Priorites
对象。
I have a rather difficult thing I would like to do and would appreciate any advice / suggestions. 我有一件很困难的事情想做,并且希望得到任何建议。 What I would like to do is to retrieve entries that have an entry that have
Priority
of "Frequency1" have Priority
of "Frequency2" or Priority
of "Frequency3" from the list entries
that I created. 我想这样做的是检索有有一个入口项
Priority
“频率1”的具有Priority
“频率2”或Priority
从列表“Frequency3”的entries
,我创建的。
public interface IJapaneseDictionaryEntry
{
int Sequence { get; }
IEnumerable<IKanji> Kanjis { get; }
IEnumerable<IReading> Readings { get; }
IEnumerable<ISense> Senses { get; }
}
Where each object contains a list of IKanji
objects 每个对象都包含
IKanji
对象的列表
public interface IKanji
{
string Text { get; }
IEnumerable<KanjiInformation> Informations { get; }
IEnumerable<Priority> Priorities { get; }
}
Here's the list: 这是清单:
List<IJapaneseDictionaryEntry> entries = dictionary.GetEntries().ToList();
Here's a view that I think might help explain the contents: 我认为以下观点可能有助于解释内容:
I hope the information here is enough as it seems difficult to explain what I need to retrieve. 我希望这里的信息足够,因为似乎很难解释我需要检索的内容。
var result = entries.Where(e => e.Kanjis.Any(k => k.Priorities.Contains(Priority.Frequency1) ||
k.Priorities.Contains(Priority.Frequency2) ||
k.Priorities.Contains(Priority.Frequency3)
)).ToList();
Considering your 2 questions, I would have made something like this: 考虑到您的两个问题,我会做这样的事情:
[Flags]
public enum Priority
{
Frequency1 = 1,
Frequency2 = 2,
Frequency3 = 4,
Frequency4 = 8
}
public interface IKanji
{
string Text { get; }
IEnumerable<KanjiInformation> Informations { get; }
Priority Priorities { get; }
}
In above consider each Priority as a bit
in an int, you can add priority by using bitwise or ( |
) : 在上面的示例中,将每个Priority视为int中的某个
bit
,您可以使用按位或( |
)添加优先级:
Priorities = Priority.Frequency1 | 优先级= Priority.Frequency1 | Priority.Frequency2 // means have both priorities
Priority.Frequency2 //表示同时具有两个优先级
To check if it has specific priority use bitwise and ( &
): 要检查它是否具有特定的优先级,请按位和(
&
)使用:
if((Priorities & Priority.Frequency1) == Priority.Frequency1
{
// it contains Priority.Frequency1
}
Then the answer you were looking for will be like: 然后,您正在寻找的答案将是:
Priority p = Priority.Frequency1 | Priority.Frequency2 | Priority.Frequency3
var result = entries.Where(e => e.Kanjis.Any(k => k.Priorities & p == p)))
.ToList();
This could be one solution: 这可能是一种解决方案:
var filteredEntries = entries.Where( // Only entries
e => e.Kanjis.Any( // which have one or more kanjis with..
a => a.Priorities.Any( // which have one or more priorities
p => p.Value == "Frequency1" // which have a value of "Frequency1"
)));
I changed your interfaces
to classes
to make it run with some example-data: 我将您的
interfaces
更改为classes
以使其与某些示例数据一起运行:
public class IJapaneseDictionaryEntry
{
public int Sequence { get; set; }
public IEnumerable<IKanji> Kanjis { get; set; }
}
public class IKanji
{
public string Text { get; set; }
public IEnumerable<Priority> Priorities { get; set; }
}
public class Priority
{
public string Value { get; set; }
}
public static void Main(string[] args)
{
// Initialize 3 objects. One has Priority we're searching
List<IJapaneseDictionaryEntry> entries = new List<IJapaneseDictionaryEntry>()
{
new IJapaneseDictionaryEntry(){ Sequence = 1, Kanjis = new List<IKanji>() { new IKanji() { Priorities = new List<Priority>() { new Priority() { Value = "Frequency1" } } } } },
new IJapaneseDictionaryEntry(){ Sequence = 2, Kanjis = new List<IKanji>() { new IKanji() { Priorities = new List<Priority>() { new Priority() { Value = "Frequency2" } } } } },
new IJapaneseDictionaryEntry(){ Sequence = 3, Kanjis = new List<IKanji>() { new IKanji() { Priorities = new List<Priority>() { new Priority() { } } } } },
};
// Here's the magic:
var filteredEntries = entries.Where( // Only entries
e => e.Kanjis.Any( // which have one or more kanjis with..
a => a.Priorities.Any( // which have one or more priorities
p => p.Value == "Frequency1" // which have a value of "Frequency1"
)));
// Let's check the output
foreach (var e in filteredEntries)
{
Console.WriteLine(e.Sequence);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.