我已经四处寻找答案,并且发现没有什么能够解决我想要做的事情。
我有2个数据表,1个从Excel工作表导入 - 另一个是在C#事件中创建的。数据表1包含每批货物或工具包的数量。符号的名称/类型是SVL-01,SVL-02等。数据表2包含这些相同的符号名称/类型,但也具有每种类型的每个单符号的权重。我试图通过数据表1迭代我的方式,计算每个项目的权重,并为每一行放置一个总和列和总和。
基本上 - 在Datatable 1 ColumnName = Datatable 2 sign name的地方,weight变量将= Datatable 2中列出的权重。
我很确定我没有正确加载图片。
答案 0 :(得分:0)
我使用previous solution作为产生更接近您需求的基础:
/* setup schema and sample data */
DataTable kit = new DataTable("Kits");
kit.Columns.Add(new DataColumn("Kit ID"));
kit.Columns.Add(new DataColumn("Sign Type"));
kit.Columns.Add(new DataColumn("Sign Count", typeof(int)));
DataTable signs = new DataTable("Signs");
signs.Columns.Add(new DataColumn("Sign Type"));
signs.Columns.Add(new DataColumn("Sign Weight", typeof(int)));
DataRow rowBuffer = kit.NewRow();
rowBuffer["Kit ID"] = "Kit 1";
rowBuffer["Sign Type"] = "SVL-01";
rowBuffer["Sign Count"] = 4;
kit.Rows.Add(rowBuffer);
rowBuffer = signs.NewRow();
rowBuffer["Sign Type"] = "SVL-01";
rowBuffer["Sign Weight"] = 44;
signs.Rows.Add(rowBuffer);
DataTable kitWeight = new DataTable();
kitWeight.Columns.Add("Kit Type", typeof(string));
kitWeight.Columns.Add("Kit Weight", typeof(int));
/* join table and produce output set */
var result = from dataRows1 in kit.AsEnumerable()
join dataRows2 in signs.AsEnumerable()
on dataRows1.Field<string>("Sign Type") equals dataRows2.Field<string>("Sign Type") into lj
from r in lj.DefaultIfEmpty()
select kitWeight.LoadDataRow(new object[]
{
dataRows1.Field<string>("Kit ID"),
dataRows1.Field<int>("Sign Count") * r.Field<int>("Sign Weight")
}, false);
/* iterate through results - we could do the multiplication here or like we did in the LINQ up above */
foreach (DataRow v in result)
Console.WriteLine(v["Kit Type"] + "\t" + v["Kit Weight"]);