简体   繁体   English

如何使用LINQ检索列表中包含一个列表的多个属性的条目?

[英]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.

相关问题 如何使用LINQ检索包含列表中列表的特定属性的条目? - How can I use LINQ to retrieve entries that contain a specific property of a list within a list? 如何使用LINQ为包含多行LIST的条目选择? - How can I select using LINQ for an entry that contains a LIST with more than one row? 如何获得列表中多于一列的总计并将其使用LINQ放入对象中? - How can I get totals for more than one column in a list and put this into an object using LINQ? 如何从对象列表到数组获得多个属性? - How can I get more than one property from an object list to an array? 如何使用LINQ查询不包含来自另一个列表的子字符串条目的字符串列表 - How to use LINQ to query list of strings that do not contain substring entries from another list 有没有一种方法可以将列表中的多个属性同时设置为“”或 null 与 a.ForEach? - Is there a way that I can set more than one property in a list to “” or null at the same time with a .ForEach? 如果有多个属性,如何对列表类型进行排序? - How to sort list type generic if more than one property? 如何在LINQ表达式中订购多个级别? - How can I order at more than one level in a LINQ expression? 我怎么能。在LINQ中包含多个级别? - How can I .Include down more than one level in LINQ? 如何根据其中的列表值使用linq过滤列表? - How can I filter a List with linq based on a value of a list within?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM