[英]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();
換句話說,我只希望在我的業務方法中根據條件修改客戶列表中的一兩個字段。 我有兩種方法可以做到這一點,
for...each
循環,遍歷客戶並修改該字段(勢在必行的方法)在 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 中不起作用)
我有這個 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();
換句話說,我只希望在我的業務方法中根據條件修改客戶列表中的一兩個字段。 我有兩種方法可以做到這一點,
for...each
循環,遍歷客戶並修改該字段(勢在必行的方法)在 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 中不起作用)
我有這個 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();
換句話說,我只希望在我的業務方法中根據條件修改客戶列表中的一兩個字段。 我有兩種方法可以做到這一點,
for...each
循環,遍歷客戶並修改該字段(勢在必行的方法)在 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 中不起作用)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.