LINQ Reference

July 21, 2017

Find items in list that matching condition

bool lHasAccepted = ItemsList.Any(itm => itm.IsAccepted.HasValue && itm.IsAccepted.Value == true);

Filter and count items in list

int lAcceptedCount = ItemsList.Where(itm => itm.IsAccepted.HasValue && itm.IsAccepted.Value == true).Count();

Convert Type and map

return await this.dbContext.Submissions.Where(sub => sub.Id == pSubmissionId).Select(sub =>
    new SubmissionResultsDto
    {
         DisplayName = sub.SalonYear.Salon.Name + " - " + sub.SalonYear.Name + " (" + sub.SalonYear.Year + ")",
         PersonId = sub.PersonId,
         SubmissionId = sub.Id,
         Entries = sub.Entries.Select(ent => new SubmissionResultsEntryDto
         {
             Id = ent.Id,
             Score = ent.Score,
             IsAwarded = ent.IsAwarded,
             IsAccepted = ent.IsAccepted,
             ImageName = ent.Image.Name,
             AwardDetails = ent.AwardDetails
          })
    }).FirstOrDefaultAsync();

Tags: linq