如何在LINQ投影中仅修改一个或两个字段?

Meh*_*ani 6 c# linq projection

我有这个LINQ查询:

List<Customers> customers = customerManager.GetCustomers();

return customers.Select(i => new Customer {
    FullName = i.FullName,
    Birthday = i.Birthday, 
    Score = i.Score,
    // Here, I've got more fields to fill
    IsVip = DetermineVip(i.Score)
}).ToList();
Run Code Online (Sandbox Code Playgroud)

换句话说,我只想在我的业务方法中根据条件修改客户列表中的一个或两个字段.我有两种方法可以做到这一点,

  1. 使用for...each循环,循环客户并修改该字段(命令式方法)
  2. 使用LINQ投影(声明方法)

在LINQ查询中是否有任何技术可用于仅修改投影中的一个属性?例如,类似于:

return customers.Select(i => new Customer {
    result = i // telling LINQ to fill other properties as it is
    IsVip = DetermineVip(i.Score) // then modifying this one property
}).ToList();
Run Code Online (Sandbox Code Playgroud)

Omi*_*ati 6

您可以使用

return customers.Select(i => {
    i.IsVip = DetermineVip(i.Score);
    return i;
}).ToList();
Run Code Online (Sandbox Code Playgroud)