Reputation: 24572
I have a List of IJapaneseDictionaryEntry
objects which are described below. Inside this are IKanji
objects.
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
public interface IKanji
{
string Text { get; }
IEnumerable<KanjiInformation> Informations { get; }
IEnumerable<Priority> Priorities { get; }
}
I selected the data with this query:
List<IJapaneseDictionaryEntry> entries = dictionary.GetEntries().ToList();
I know how to do a simple query like this:
var a = entries.SelectMany(x => x.Kanjis);
and how to select just the text:
var a = entries.SelectMany(x => x.Kanjis).Select(x => x.Text);
But what I would like to see the text and the priorities and I am not sure how to do that. What I would like to do is to get a flat output like this:
kanji1 ichimango1 Newspaper1 frequency10
How can I select, order and flatten out the priorities with a select?
Something like this:
var a = entries.SelectMany(x => x.Kanjis)
.Select(x => x.Text, Priorities1, Priorities2, Priorities3);
Note that there may be no entry for Priorities1, Priorities2 or Priorities3
Upvotes: 0
Views: 1581
Reputation: 37000
You would need to create an anonymous object with the properties you want to return:
var a = entries.SelectMany(x => x.Kanjis)
.Select(x => new { x.Text, x.Priorities });
As per your EDIT: If you want to get only those Kanjis
which have a Priority
at all, you need to add a Where
after the Select
:
var a = entries.SelectMany(x => x.Kanjis)
.Select(x => new { x.Text, x.Priorities })
.Where(x => x.Priorities.Any());
However I would suggest to call ToList
on x.Priorities
in order to avoid iterating the same collection twice just to determine if there are elements at all. Technically you could also use the Where
in front of the Select
, but in this case you´d lose the ability to call ToList
to avoid those multiple iterations.
Upvotes: 4