c# – 如何在LINQ投影中仅修改一个或两个字段?
发布时间:2020-12-15 08:41:56 所属栏目:百科 来源:网络整理
导读:我有这个LINQ查询: ListCustomers 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.
我有这个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(); 换句话说,我只想在我的业务方法中根据条件修改客户列表中的一个或两个字段.我有两种方法可以做到这一点, >使用for …每个循环,循环客户并修改该字段(命令式方法) 在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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |