我有这个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();
换句话说,在我的业务方法中,我只想根据条件修改客户列表中的一个或两个字段。我有两种方法可以做到这一点:
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();
customer.FullName += "foo"
并且你最终枚举了两次集合,你会得到一些非常有趣的结果。 - lc.