我想验证两个数据表的数量列。下面给出的是示例数据表。我想从 dtItems 中获取每个项目数量的总和,并与 dtMasterItem 中相同项目的数量进行比较。如果 dtItems 数量大于 dtMasterItem 数量,那么我希望显示消息,例如 项目代码的可用数量为1000 < / p>
dtMasterItem
ItemCode Quantity
A 3000
B 5000
C 6000
dtItems
ItemCode Quantity
A 2000
A 1000
A 500
B 3000
B 2000
C 6000
根据我的逻辑,如果我们验证上面的表数据,输出将类似于 A的可用数量为3000
答案 0 :(得分:3)
你的问题不是很清楚,但根据我的理解,这应该为你做。
foreach (DataRow rowMasterItems in dtMasterItem.Rows)
{
int lintSumOfItems = 0;
foreach (DataRow rowItems in dtItems.Rows)
{
if (rowMasterItems["ItemCode"].ToString().Equals(rowItems["ItemCode"].ToString()))
{
lintSumOfItems += Convert.ToInt16(rowItems["Quantity"]);
}
}
if (Convert.ToInt16(rowMasterItems["Quantity"]) >= lintSumOfItems)
MessageBox.Show("OK");
else
MessageBox.Show("Available quantity is " + Convert.ToInt16(rowMasterItems["Quantity"]) + " for itemcode: " + rowMasterItems["ItemCode"].ToString());
}
答案 1 :(得分:1)
试试这段代码,它对我有用:
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("ItemCode", typeof(string));
dt1.Columns.Add("Quantity", typeof(int));
dt2.Columns.Add("ItemCode", typeof(string));
dt2.Columns.Add("Quantity", typeof(int));
dt1.Rows.Add("A", 3000);
dt1.Rows.Add("B", 5000);
dt1.Rows.Add("C", 6000);
dt2.Rows.Add("A", 2000);
dt2.Rows.Add("A", 1000);
dt2.Rows.Add("A", 500);
dt2.Rows.Add("B", 3000);
dt2.Rows.Add("B", 2000);
dt2.Rows.Add("c", 6000);
var query = from row in dt2.AsEnumerable()
group row by row.Field<string>("ItemCode") into grp
select new
{
ItemCode = grp.Key,
Quantity = grp.Sum(r => r.Field<int>("Quantity"))
};
int i = 0;
foreach (var item in query)
{
if (Convert.ToInt32(item.Quantity) > Convert.ToInt32(dt1.Rows[i]["Quantity"]))
{
Console.WriteLine("Available quantity is "+ dt1.Rows[i]["Quantity"].ToString()+ " for "+item.ItemCode);
}
i++;
}
Console.Read();
答案 2 :(得分:1)
使用linq查询
尝试此操作 DataTable dt1 = new DataTable("dtMasterItem");
dt1.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "ItemCode" });
dt1.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "Quantity" });
DataTable dt2 = new DataTable("dtItems");
dt2.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "ItemCode" });
dt2.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "Quantity" });
dt1.Rows.Add("A", 3000);
dt1.Rows.Add("B", 5000);
dt1.Rows.Add("C", 6000);
dt2.Rows.Add("A", 2000);
dt2.Rows.Add("A", 1000);
dt2.Rows.Add("A", 500);
dt2.Rows.Add("B", 3000);
dt2.Rows.Add("B", 2000);
dt2.Rows.Add("C", 6000);
var result = (from k in
(from x in dt2.AsEnumerable()
group x by x["ItemCode"] into entryGroup
select new
{
ItemCode = entryGroup.Key,
Quantity = entryGroup.Sum(i => Convert.ToInt32(i["Quantity"]))
})
join y in dt1.AsEnumerable() on k.ItemCode equals y["ItemCode"] into DataGroup
from item in DataGroup.DefaultIfEmpty()
select new
{
ItemCode = k.ItemCode,
Q1 = k.Quantity,
Q2 = item["Quantity"],
Remarks = Convert.ToInt32(item["Quantity"]) < k.Quantity ? "Available quantity is " + k.Quantity + " for " + k.ItemCode : null
}).AsEnumerable().Where(i => i.Remarks != null);
输出: