我想知道是否有人可以幫助我將以下資料(從資料庫中)轉換為 List。:
| 洛瑞德 | 產品編號 | 數量型別 | 數量 | 評論 | 評論 |
|---|---|---|---|---|---|
| 1 | 4090 | 3 | 2 | F | |
| 1 | 4153 | 3 | 1 | F | |
| 1 | 4153 | 3 | 1 | F | |
| 1 | 4153 | 3 | 1 | F | |
| 1 | 4153 | 3 | 1 | F | |
| 1 | 4153 | 3 | 1 | F | |
| 1 | 31068 | 3 | 2 | F | |
| 1 | 31069 | 3 | 2 | F | |
| 1 | 31173 | 3 | 2 | F | |
| 2 | 17 | 0 | 1.000 | F | |
| 2 | 150 | 3 | 6.000 | F | |
| 2 | 216 | 3 | 6.000 | F | |
| 2 | 278 | 2 | 1.020 | F | |
| 2 | 398 | 2 | 1.125 | F | |
| 2 | 398 | 2 | 1.090 | F | |
| 2 | 398 | 2 | 0 | 噸 | 必須成熟 |
| 2 | 431 | 2 | 3.000 | F | |
| 2 | 436 | 3 | 1.000 | F | |
| 2 | 446 | 0 | 1 | F | |
| 2 | 446 | 2 | 3.045 | F | |
| 2 | 451 | 3 | 2.000 | F | |
| 2 | 457 | 3 | 1.000 | F | |
| 2 | 458 | 3 | 4.000 | F | |
| 2 | 478 | 3 | 1.000 | F | |
| 2 | 510 | 2 | 1.140 | F | |
| 2 | 518 | 3 | 3.000 | F | |
| 2 | 518 | 3 | 4.000 | F | |
| 2 | 550 | 2 | 1.170 | F | |
| 2 | 550 | 3 | 1.000 | F |
進入下面的物件。
public class View
{
public List<LorryLoading> Report
}
public class LorryLoading
{
public int LorryId
public List<Product> Product
}
public class Product
{
public int ProductId
public decimal Qty0 sum(Quantity) WHERE QtyType = 0
public decimal Qty2 sum(Quantity) WHERE QtyType = 2
public decimal Qty3 sum(Quantity) WHERE QtyType = 3
public List<string> Comments
}
是否可以使用單個投影?在分配串列時遇到問題。
另外,我可以在其中學習此類轉換的任何鏈接嗎?到目前為止,我發現的例子相當簡單或解釋得很差。
uj5u.com熱心網友回復:
您要問的是轉換,因此沒有簡單的投影。你可能可以用一個超級復雜的陳述句來做到這一點,這會讓你的同事在必須維護時換作業:) 我的建議是不要試圖給校園留下深刻印象,而是專注于干凈的代碼:
var lorryLoadingReport = new List<ReportData>();
//Presume you already have data into a transport object and know how to read that, as it wasn't your question
var view = new View
{
Report = new List<LorryLoading>()
};
var listOf = new List<Product>();
//Lets not over complicate our mind, just keep it simple, let's get the products and the lorries
var productsList = lorryLoadingReport.Select(x => x.ProductId).ToList();
var lorriesList = lorryLoadingReport.Select(x => x.LorryId).ToList();
foreach (var lorry in lorriesList)
{
//We'll need an entry even if the list will be empty
var lorryLoading = new LorryLoading
{
LorryId = lorry,
Product = new List<Product>()
};
//Get the respective subset
var lorryLoadingReportPerLorry = lorryLoadingReport.Where(l => l.LorryId == lorry);
if (lorryLoadingReportPerLorry.Any())
{
foreach (var productId in productsList)
{
//And add a product with all 4 sums iteratively
var p = new Product
{
ProductId = productId,
Comments = new List<string>()
};
var commentsList = lorryLoadingReport.Where(x => x.ProductId == productId && x.IsComment == "t").Select(s => s.Comment).ToList();
if(commentsList.Any())
p.Comments.AddRange(commentsList);
p.Qty0 = lorryLoadingReport.Where(x => x.Qty == 0).Sum(y => y.Qty);
p.Qty2 = lorryLoadingReport.Where(x => x.Qty == 2).Sum(y => y.Qty);
p.Qty3 = lorryLoadingReport.Where(x => x.Qty == 3).Sum(y => y.Qty);
lorryLoading.Product.Add(p);
}
}
view.Report.Add(lorryLoading);
}
uj5u.com熱心網友回復:
您可以使用 LinqAggregate進行自定義聚合。這稍微復雜一些,因為您在這里進行了雙重聚合。
請注意,這可能僅適用于 Linq-to-Objects。
var report = data
.GroupBy(row => row.Lorryid)
.Select(l => new LorryLoading {
LorryId = l.Key,
Product = l
.GroupBy(row => row.productid)
.Select(p => p.Aggregate(
new Product {ProductId = p.Key}, // this is the starting object
(prod, row) => { // this aggregates it up
if(comment != null)
prod.Comments.Add(row.comment);
if(QtyType == 0)
prod.Qty0 = row.Quantity;
else if(QtyType == 2)
prod.Qty2 = row.Quantity;
else if(QtyType == 3)
prod.Qty3 = row.Quantity;
})
.ToList()
}).ToList();
var view = new View {Report = report};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323857.html
