简体   繁体   中英

Entity Framework: Modify only one field in Select projection

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,

  1. Using for...each loop, to loop over customers and modify that field (imperative approach)
  2. 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?

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,

  1. Using for...each loop, to loop over customers and modify that field (imperative approach)
  2. 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?

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,

  1. Using for...each loop, to loop over customers and modify that field (imperative approach)
  2. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM