Linq - 如何在不使用 new 的情况下选择多个字段

本文关键字:情况下 选择 字段 new Linq | 更新日期: 2024-10-21 20:39:14

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 和 UserSelectionControl

没有传递给它们的 SectionControlTab 的相同命名字段,我已经尝试了两个单独的选择语句,但第二个看不到 UserSelectionControl,我还有另一个字段我想复制。 如果调用 x.LinkedWorkControl...再次这将是重复的。

上面使用复杂的定义类型,我只需要从父级读取并更新视图模型的子项。

如何在不使用new的情况下更新多个字段????

Linq - 如何在不使用 new 的情况下选择多个字段

您可以使用

Tuple.Create但这基本上隐藏了问题(并且在 C# 7 之前,语法看起来更差(。C# 表示一组字段的方式是作为对象(从 C# 6.0 开始(。使用 new 创建对象。没有办法解决这个问题(使用 Linq(。

相关文章: