将两个数据表中,并通过使用LINQ获得从第二个表的金额 [英] Combining two datatable and getting the amount from second table by using linq
本文介绍了将两个数据表中,并通过使用LINQ获得从第二个表的金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表,如下图所示。
表1
╔══════════════╦═════╦════╗
║║名年龄║║ID
╠══════════════╬═════╬════╣
║名字1║║20║1
║名字2║║21║2
║名字3║║22║3
║名字4║║23║4
║名字5║║24║5
║名字6║║25║6
║姓7║║26║7
║姓8║║27║8
║名字9║║28║9
║姓10║║29║10
╚══════════════╩═════╩════╝
表2
╔════════╦══════════╗
║║LINKID数量║
╠════════╬══════════╣
║║1 1║
║║1 1║
║║2 1║
║║2 1║
║║2 1║
║║3 1║
║║3 1║
║║3 1║
║║3 1║
║║4 1║
║║4 1║
║║4 1║
║║4 1║
║║4 1║
║║5 1║
║║5 1║
║║5 1║
║║5 1║
║║5 1║
║║5 1║
║║6 1║
║║6 1║
║║6 1║
║║6 1║
║║6 1║
║║6 1║
║║6 1║
║║7 1║
║║7 1║
║║7 1║
║║7 1║
║║7 1║
║║7 1║
║║7 1║
║║7 1║
║║8 1║
║║8 1║
║║8 1║
║║8 1║
║║8 1║
║║8 1║
║║8 1║
║║8 1║
║║8 1║
║║9 1║
║║9 1║
║║9 1║
║║9 1║
║║9 1║
║║9 1║
║║9 1║
║║9 1║
║║9 1║
║║9 1║
║║10║1
║║10║1
║║10║1
║║10║1
║║10║1
║║10║1
║║10║1
║║10║1
║║10║1
║║10║1
║║10║1
╚════════╩══════════╝
结果表看起来是这样的。
<$p$p><$c$c>╔══════════════╦═════╦════╦═════╗ ║║名年龄║║ID数量║ ╠══════════════╬═════╬════╬═════╣ ║名字1║║20║1 2║ ║名字2║║21║2 3║ ║名字3║║22║3 4║ ║名字4║║23║4 5║ ║名字5║║24║5 6║ ║名字6║║25║6 7║ ║姓7║║26║7 8║ ║姓8║║27║8 9║ ║名字9║║28║9 10║ ║姓10║║29║10║11 ╚══════════════╩═════╩════╩═════╝
我要在1个LINQ语句完成这一点。 (如果完全不可能,2语句)
我已经这么远过我的发言。
VAR resultQuery =从在dtbl1.AsEnumerable()
加入b在dtbl2.AsEnumerable()
上a.Field&其中; INT&GT;(ID)等于b.Field&其中; INT&GT(链路ID)
选择新
{
ID =一[ID],
名称=一[名称],
年龄=一[年龄],
数量= B [数量]
}
;
该声明将结合2表,但我不知道如何继续计数的数量。
你知道吗?
如果我必须使用第二个查询,它已经完成。
VAR secondResultQuery =从行resultQuery
组一行通过新的{row.ID,row.Name,row.Age}为G
选择新
{
g.Key.ID,
g.Key.Name,
g.Key.Age,
totalCount = g.Sum(数量=&GT;(INT)count.Qty)
};
解决方案
VAR的ResultSet =从在dtbl1.AsEnumerable()
加入b在dtbl2.AsEnumerable()
在a.ID等于b.LinkID
一组由新
{
a.Name,
a.Age,
援助
}为G
选择新
{
g.Key.Name,
g.Key.Age,
g.Key.ID,
g.Count()
};
查询与 DataTable的工作
VAR resultSetTicketNo =从在dtbl1.AsEnumerable()
加入b在dtbl2.AsEnumerable()
上a.Field&其中; INT&GT;(ID)等于b.Field&其中; INT&GT(链路ID)
一组由新
{
年龄=一[年龄],
ID =一[ID],
名称=一[名称]
}为G
选择新
{
g.Key.Name,
g.Key.Age,
g.Key.ID,
计数器= g.Count()
};
的foreach(在resultSetTicketNo VAR一)
{
回复于(a.ID +〜+ a.Name +〜+ a.Age +〜+ a.Counter +&所述峰; br /&gt;中);
}
回复于(&LT; BR /&GT;&LT; BR /&GT; resultSetTicketNo&LT结束; BR /&GT;&LT; BR /&gt;中);
I have two tables as shown below.
Table 1
╔══════════════╦═════╦════╗
║ NAME ║ AGE ║ ID ║
╠══════════════╬═════╬════╣
║ firstname 1 ║ 20 ║ 1 ║
║ firstname 2 ║ 21 ║ 2 ║
║ firstname 3 ║ 22 ║ 3 ║
║ firstname 4 ║ 23 ║ 4 ║
║ firstname 5 ║ 24 ║ 5 ║
║ firstname 6 ║ 25 ║ 6 ║
║ firstname 7 ║ 26 ║ 7 ║
║ firstname 8 ║ 27 ║ 8 ║
║ firstname 9 ║ 28 ║ 9 ║
║ firstname 10 ║ 29 ║ 10 ║
╚══════════════╩═════╩════╝
Table 2
╔════════╦══════════╗
║ LINKID ║ QUANTITY ║
╠════════╬══════════╣
║ 1 ║ 1 ║
║ 1 ║ 1 ║
║ 2 ║ 1 ║
║ 2 ║ 1 ║
║ 2 ║ 1 ║
║ 3 ║ 1 ║
║ 3 ║ 1 ║
║ 3 ║ 1 ║
║ 3 ║ 1 ║
║ 4 ║ 1 ║
║ 4 ║ 1 ║
║ 4 ║ 1 ║
║ 4 ║ 1 ║
║ 4 ║ 1 ║
║ 5 ║ 1 ║
║ 5 ║ 1 ║
║ 5 ║ 1 ║
║ 5 ║ 1 ║
║ 5 ║ 1 ║
║ 5 ║ 1 ║
║ 6 ║ 1 ║
║ 6 ║ 1 ║
║ 6 ║ 1 ║
║ 6 ║ 1 ║
║ 6 ║ 1 ║
║ 6 ║ 1 ║
║ 6 ║ 1 ║
║ 7 ║ 1 ║
║ 7 ║ 1 ║
║ 7 ║ 1 ║
║ 7 ║ 1 ║
║ 7 ║ 1 ║
║ 7 ║ 1 ║
║ 7 ║ 1 ║
║ 7 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 8 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 9 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
║ 10 ║ 1 ║
╚════════╩══════════╝
the result table looks like this.
╔══════════════╦═════╦════╦═════╗
║ NAME ║ AGE ║ ID ║ QTY ║
╠══════════════╬═════╬════╬═════╣
║ firstname 1 ║ 20 ║ 1 ║ 2 ║
║ firstname 2 ║ 21 ║ 2 ║ 3 ║
║ firstname 3 ║ 22 ║ 3 ║ 4 ║
║ firstname 4 ║ 23 ║ 4 ║ 5 ║
║ firstname 5 ║ 24 ║ 5 ║ 6 ║
║ firstname 6 ║ 25 ║ 6 ║ 7 ║
║ firstname 7 ║ 26 ║ 7 ║ 8 ║
║ firstname 8 ║ 27 ║ 8 ║ 9 ║
║ firstname 9 ║ 28 ║ 9 ║ 10 ║
║ firstname 10 ║ 29 ║ 10 ║ 11 ║
╚══════════════╩═════╩════╩═════╝
I wish to finish this within 1 linq statement. (if totally impossible, 2 statements)
I have worked my statement this far.
var resultQuery = from a in dtbl1.AsEnumerable()
join b in dtbl2.AsEnumerable()
on a.Field<int>("ID") equals b.Field<int>("LinkID")
select new
{
ID = a["ID"],
Name = a["Name"],
Age = a["Age"],
Qty = b["Quantity"]
}
;
That statement will combine 2 tables, but I dunno how to continue to count the for the quantity.
Any idea?
If I must use a second query, It has been done.
var secondResultQuery = from row in resultQuery
group row by new { row.ID, row.Name, row.Age } into g
select new
{
g.Key.ID,
g.Key.Name,
g.Key.Age,
totalCount = g.Sum(count => (int)count.Qty)
};
解决方案
var resultSet = from a in dtbl1.AsEnumerable()
join b in dtbl2.AsEnumerable()
on a.ID equals b.LinkID
group a by new
{
a.Name,
a.Age,
a.ID
} into g
select new
{
g.Key.Name,
g.Key.Age,
g.Key.ID,
g.Count()
};
Query to work with DataTable
var resultSetTicketNo = from a in dtbl1.AsEnumerable()
join b in dtbl2.AsEnumerable()
on a.Field<int>("ID") equals b.Field<int>("LinkID")
group a by new
{
Age = a["Age"],
ID = a["ID"],
Name = a["Name"]
} into g
select new
{
g.Key.Name,
g.Key.Age,
g.Key.ID,
Counter=g.Count()
};
foreach (var a in resultSetTicketNo)
{
Response.Write(a.ID + "~" + a.Name + "~" + a.Age + "~" + a.Counter + "<br/>");
}
Response.Write("<br/><br/>end of resultSetTicketNo<br/><br/>");
这篇关于将两个数据表中,并通过使用LINQ获得从第二个表的金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文