简体   繁体   中英

How can I select using LINQ for an entry that contains a LIST with more than one row?

I have a List of IJapaneseDictionaryEntry objects:

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; }
}

Which I am do a LINQ query on like this:

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

Is there a way that I can filter this so I only retrieve entries that have at least one priority?

You can use Eumerable.Where :

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

Or in query syntax:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM