I have this EF query:
var customers = customers.SelectMany(ps => ps.ProductSales.Select(c => new Customer() {
return customers.Select(i => new Customer {
FullName = i.FullName,
Birthday = i.Birthday,
Score = i.Score,
// Here, I've got more fields to fill
ProductSales= new List<ProductSales>() { p }
}).ToList();
In other words, I only want one or two fields of the list of the customers to be modified based on a condition, in my business method. I've got two ways to do this,
- Using
for...each
loop, to loop over customers and modify that field (imperative approach)
- Using LINQ projection (declarative approach)
Is there any technique to be used in LINQ query, to only modify one property in projection? For example, something like:
return customers.SelectMany(ps => ps.ProductSales.Select(c => new Customer()
.Select(i => new Customer {
result = i // telling LINQ to fill other properties as it is
ProductSales= new List<ProductSales>() { p } // then modifying this one property
}).ToList();
Actual class contains 30 members, don't want to rewrite everything
*Please keep result in IQueryable format
Trying to keep as One Select, so I can grab nested properties if possible.
Currently using EF Core 3.1
Resources:
based off this question using regular linq in 2010 (this syntax does not work in EF Core 3.1)
How to modify only one or two field(s) in LINQ projections?
question from:
https://stackoverflow.com/questions/66057069/entity-framework-modify-only-one-field-in-select-projection 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…