c# - 如何查询/将数据转换为所需的格式
问题描述
我想知道是否有人可以帮助我将以下数据(来自数据库)转换为 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
}
是否可以使用单个投影?在分配列表时遇到问题。
另外,我可以学习这种转换的任何链接?到目前为止,我发现的示例相当简单或解释不清。
解决方案
您要问的是转换,因此没有简单的投影。您可能可以使用一个超级复杂的语句来做到这一点,这将使您的同事在必须维护时更换工作:) 我的建议是不要试图给校园留下深刻印象,而是专注于干净的代码:
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);
}
推荐阅读
- r - R中数据框中的S4对象列表
- reactjs - 我收到 TypeError: Cannot destructure property 'hasNextPage' of '{}' 因为当我尝试使用 wordpress 构建 gatsby 应用程序时它为空
- node.js - 使用 Node js 对 GCP Bucket 中的 pdf 执行预测:无效参数
- python - 我们不能在 python 方法的争论中初始化字典吗?
- react-native - 反应本机 panResponder onRelease 事件窃取可触摸的不透明度 onpress 事件
- python - 随机洗牌总是返回无
- react-native - 在本机反应中从下到上填充自定义图像
- javascript - 使用 CSS 使 DOM 滚动以显示所需的内容
- macos - SSH 到远程服务器无法连接
- android - 如何让 RecyclerView 显示项目?