簡體   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