Andrew Day
Andrew Day

Reputation: 607

Linq - how do I select several fields without using new

model.SectionControlTabs.ForEach(x =>
{
    x.EffectiveDate = x.EffectiveDate?.Split(' ')[0];
    x.LinkedWorkControlEffectiveDate = x.EffectiveDate != null? model.WorkControls
    .Where(y => DateTime.Parse(y.EffectiveDate) <= DateTime.Parse(x.EffectiveDate))
    .OrderByDescending(y => DateTime.Parse(y.EffectiveDate))
    .Select(y =>  new { CoverSelectionControl = y.CoverSelectionControl.ToString(),
        UserSelectionControl = y.UserSelectionControl.ToString()})
    .DefaultIfEmpty()
    .First().ToString(): "";
});

CoverSelectionControl and UserSelectionControl are not passed to the same named fields of their SectionControlTab, I have tried two seperate select statements but the second cant see the UserSelectionControl and I also have another field I wish to copy. If call the x.LinkedWorkControl... again it will be a duplication.

Above using complex defined types I just need to read from a parent and update the children for a viewModel.

How do I update a number of fields without using new????

Upvotes: 2

Views: 134

Answers (1)

codekaizen
codekaizen

Reputation: 27419

You could use Tuple.Create but that basically hides the problem (and, until C# 7, has worse looking syntax). C#'s way of expressing a set of fields is as an object (as of C# 6.0). You create an object with new. There's no way around this (using Linq).

Upvotes: 2

Related Questions