如何选择对包含LIST且行多的条目使用LINQ?

时间:2019-04-05 08:22:12

标签: c# linq

我有IJapaneseDictionaryEntry个对象的列表:

public interface IJapaneseDictionaryEntry
{
    int Sequence { get; }
    IEnumerable<IKanji> Kanjis { get; }
    IEnumerable<IReading> Readings { get; }
    IEnumerable<ISense> Senses { get; }
}

public interface IKanji
{
    string Text { get; }
    IEnumerable<KanjiInformation> Informations { get; }
    IEnumerable<Priority> Priorities { get; }
}

我要像这样对LINQ进行查询:

var a = entries.SelectMany(x => x.Kanjis)
               .Select(x => new { x.Text, x.Priorities });

有没有一种方法可以对此进行过滤,以便仅检索具有至少一个优先级的条目?

1 个答案:

答案 0 :(得分:2)

您可以使用Eumerable.Where

var a = entries.SelectMany(x => x.Kanjis)
               .Where(x => x.Priorities.Any())
               .Select(x => new { x.Text, x.Priorities });

或使用查询语法:

var e =
    from entry in entries
    from kanji in entry.Kanjis
    where kanji.Priorities.Any()
    select new { kanji.Text, kanji.Priorities };