简体   繁体   English

实体框架:仅修改 Select 投影中的一个字段

[英]Entity Framework: Modify only one field in Select projection

I have this EF query:我有这个 EF 查询:

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)使用for...each循环,遍历客户并修改该字段(势在必行的方法)
  2. Using LINQ projection (declarative approach)使用 LINQ 投影(声明式方法)

Is there any technique to be used in LINQ query, to only modify one property in projection?在 LINQ 查询中是否使用任何技术,仅修改投影中的一个属性? 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实际 class 包含 30 个成员,不想重写所有内容

*Please keep result in IQueryable format *请将结果保存为 IQueryable 格式

Trying to keep as One Select, so I can grab nested properties if possible.尝试保持为一个 Select,因此如果可能,我可以获取嵌套属性。

Currently using EF Core 3.1当前使用 EF Core 3.1

Resources: based off this question using regular linq in 2010 (this syntax does not work in EF Core 3.1)资源:基于this question using regular linq in 2010(此语法在 EF Core 3.1 中不起作用)

How to modify only one or two field(s) in LINQ projections? 如何仅修改 LINQ 投影中的一个或两个字段?

I have this EF query:我有这个 EF 查询:

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)使用for...each循环,遍历客户并修改该字段(势在必行的方法)
  2. Using LINQ projection (declarative approach)使用 LINQ 投影(声明式方法)

Is there any technique to be used in LINQ query, to only modify one property in projection?在 LINQ 查询中是否使用任何技术,仅修改投影中的一个属性? 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实际 class 包含 30 个成员,不想重写所有内容

*Please keep result in IQueryable format *请将结果保存为 IQueryable 格式

Trying to keep as One Select, so I can grab nested properties if possible.尝试保持为一个 Select,因此如果可能,我可以获取嵌套属性。

Currently using EF Core 3.1当前使用 EF Core 3.1

Resources: based off this question using regular linq in 2010 (this syntax does not work in EF Core 3.1)资源:基于this question using regular linq in 2010(此语法在 EF Core 3.1 中不起作用)

How to modify only one or two field(s) in LINQ projections? 如何仅修改 LINQ 投影中的一个或两个字段?

I have this EF query:我有这个 EF 查询:

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)使用for...each循环,遍历客户并修改该字段(势在必行的方法)
  2. Using LINQ projection (declarative approach)使用 LINQ 投影(声明式方法)

Is there any technique to be used in LINQ query, to only modify one property in projection?在 LINQ 查询中是否使用任何技术,仅修改投影中的一个属性? 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实际 class 包含 30 个成员,不想重写所有内容

*Please keep result in IQueryable format *请将结果保存为 IQueryable 格式

Trying to keep as One Select, so I can grab nested properties if possible.尝试保持为一个 Select,因此如果可能,我可以获取嵌套属性。

Currently using EF Core 3.1当前使用 EF Core 3.1

Resources: based off this question using regular linq in 2010 (this syntax does not work in EF Core 3.1)资源:基于this question using regular linq in 2010(此语法在 EF Core 3.1 中不起作用)

How to modify only one or two field(s) in LINQ projections? 如何仅修改 LINQ 投影中的一个或两个字段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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