I am trying to rewrite and integrate the following code in to a single statement:
So instead of having to do this:
var dbList = await _context.ProfileSections.Select(p => new
{
p.Id,
p.Description,
p.InformationText
}).ToListAsync();
var localisedList = dbList.Select(p => new
{
p.Id,
Description = p.Description.Translate(),
Information = p.InformationText.Translate()
}).ToList();
I assume I could use if I strongly type to a model and use a foreach, something like:
var dbList = await _context.ProfileSections.Select(p => new ProfileSectionModel
{
p.Id,
p.Description,
p.InformationText
}).ToListAsync();
var localisedList = dbList.ForEach(l => {
l.Description = l.Description.Translate();
l.InformationText = l.InformationText.Translate();
}).ToList();
However then get error message: Operator '.' cannot be applied to operand of type 'void'
So is there an easier to combine the two statements? Or will I always have to as the first returns an anonymouse type. Unsure what is the correct way to handle this?
EDIT
Could use the following but was then getting issue with the dynamic proxies that EF uses. I guess thats why its selecting into an anonymous type?
var dbList = await _context.ProfileSections.ToListAsync();
foreach (var item in dbList)
{
item.Description = item.Description.Translate(),
item.Information = item.InformationText.Translate()
}