Home > Software design >  How to convert IQueryable<IList<AnimeTitle>> to IQueryable<AnimeTitle>
How to convert IQueryable<IList<AnimeTitle>> to IQueryable<AnimeTitle>

Time:05-01

I need to get a collection by id, it has IList<AnimeTitle> AnimeTitles, which I need to convert to IQueryable<AnimeTitle> and project into BriefTitleVW to end up with IQueryable<BriefTitleVW>.

In the sample code, I do the conversion using AutoMapper ProjectTo<BriefTitleVM>(_mapper.ConfigurationProvider), but this is not possible because this method can only convert IQueryable<AnimeTitle> to IQueryable<BriefTitleVM>.

So it comes down to converting IQueryable<IList<AnimeTitle>> to IQueryable<AnimeTitle>.

Question: how to do that?

var titles = await _dbContext.AnimeCollections
                             .Where(c => c.Id == request.CollectionId && c.UserId == request.UserId)
                             .Select(c => c.AnimeTitles)
                             .ProjectTo<BriefTitleVM>(_mapper.ConfigurationProvider)

CodePudding user response:

You can change:

.Select(c => c.AnimeTitles)

To:

.SelectMany(c => c.AnimeTitles)

When you have a "list of lists of X" (you have a list of AnimeCollection objects inside db.AnimeCollections, and each AnimeCollection has a list of AnimeTitle objects in its AnimeTitles) you can turn it into a straight "list of X" by using SelectMany. SelectMany, conceptually does this:

List<AnimeTitle> result = new();
foreach(AnimeCollection c in db.AnimeCollections)
  result.AddRange(c.AnimeTitles);
return result;

So your general pattern is to feed .SelectMany with a lambda that chooses a collection on each object in a list, and it churns out a single list of all the items in all the collections

//one list of all child items
parentCollection.SelectMany(item => item.ChildCollection)
  • Related