繁体   English   中英

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

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

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

换句话说,我只希望在我的业务方法中根据条件修改客户列表中的一两个字段。 我有两种方法可以做到这一点,

  1. 使用for...each循环,遍历客户并修改该字段(势在必行的方法)
  2. 使用 LINQ 投影(声明式方法)

在 LINQ 查询中是否使用任何技术,仅修改投影中的一个属性? 例如,类似:

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();

实际 class 包含 30 个成员,不想重写所有内容

*请将结果保存为 IQueryable 格式

尝试保持为一个 Select,因此如果可能,我可以获取嵌套属性。

当前使用 EF Core 3.1

资源:基于this question using regular linq in 2010(此语法在 EF Core 3.1 中不起作用)

如何仅修改 LINQ 投影中的一个或两个字段?

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

换句话说,我只希望在我的业务方法中根据条件修改客户列表中的一两个字段。 我有两种方法可以做到这一点,

  1. 使用for...each循环,遍历客户并修改该字段(势在必行的方法)
  2. 使用 LINQ 投影(声明式方法)

在 LINQ 查询中是否使用任何技术,仅修改投影中的一个属性? 例如,类似:

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();

实际 class 包含 30 个成员,不想重写所有内容

*请将结果保存为 IQueryable 格式

尝试保持为一个 Select,因此如果可能,我可以获取嵌套属性。

当前使用 EF Core 3.1

资源:基于this question using regular linq in 2010(此语法在 EF Core 3.1 中不起作用)

如何仅修改 LINQ 投影中的一个或两个字段?

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

换句话说,我只希望在我的业务方法中根据条件修改客户列表中的一两个字段。 我有两种方法可以做到这一点,

  1. 使用for...each循环,遍历客户并修改该字段(势在必行的方法)
  2. 使用 LINQ 投影(声明式方法)

在 LINQ 查询中是否使用任何技术,仅修改投影中的一个属性? 例如,类似:

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();

实际 class 包含 30 个成员,不想重写所有内容

*请将结果保存为 IQueryable 格式

尝试保持为一个 Select,因此如果可能,我可以获取嵌套属性。

当前使用 EF Core 3.1

资源:基于this question using regular linq in 2010(此语法在 EF Core 3.1 中不起作用)

如何仅修改 LINQ 投影中的一个或两个字段?

暂无
暂无

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

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