gpt4 book ai didi

c# - 如何只修改 LINQ 投影中的一个或两个字段?

转载 作者:太空狗 更新时间:2023-10-29 21:34:37 26 4
gpt4 key购买 nike

我有这个 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();

最佳答案

你可以使用

return customers.Select(i => {
i.IsVip = DetermineVip(i.Score);
return i;
}).ToList();

关于c# - 如何只修改 LINQ 投影中的一个或两个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com