使用LINQ加入两个数据表 [英] Join two datables using LINQ

查看:93
本文介绍了使用LINQ加入两个数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts,



我有两个数据表

dt1列有以下列



Hello Experts,

I have two Datatables
dt1 has columns as below

Item	     Description	Size	 Bin	MoveQty	ToBin	Comment
AD1952203863	ADVANTA CLT 120/116Q E	AQ042	38	AF118  Reduced Bin AQ042
AD1952203863	ADVANTA CLT 120/116Q E	AQ042	2	AX050	Cleared Bin AQ042
AD1952204763	ADVANTA CLT 120/116Q E	AK088	16	AI110	Reduced Bin AK088







第二个数据表如下






And second datatable as below

Bin	                 Item	          Action
AQ042	                      AD1952203863	  Reduced,Cleared
AR085(Committed Remains)	AD1952233863	Cleared
AR106	                       AD1952234760	Cleared





现在我想要一个LINQ查询来获得合并结果,例如





Now I want a LINQ query to get combined results like

Bin	Item	Description 	Size	MoveQty	ToBin	Action







我尝试了一个无效的查询。




I have tried one query which is not working.

Dim JoinResult = _
         From o In dtcleared.AsEnumerable _
         Join a In dtNew.AsEnumerable _
         On a.Field(Of String)("Item") Equals o.Field(Of String)("Item") _
       Select New With _
       { _
         Key .Item = a.Field(Of String)("Item"), _
         Key .Bin = a.Field(Of String)("Bin"), _
         Key .Size = a.Field(Of String)("Size"), _
         Key .Description = a.Field(Of String)("Description"), _
         Key .MoveQty = a.Field(Of String)("Move Qty"), _
         Key .Action = o.Field(Of String)("Action")
       }





有人可以帮帮我吗?



Can someone help me please?

推荐答案

终于找到了解决方案。



Finally found solution.

Dim JoinResult1 = _
         (From o In dtcleared.AsEnumerable _
         Join a In dtNew.AsEnumerable _
         On a.Field(Of String)("Item") Equals o.Field(Of String)("Item") _
       Select New With _
       { _
         Key .Item = a.Field(Of String)("Item"), _
         Key .Bin = a.Field(Of String)("Bin"), _
         Key .Size = a.Field(Of String)("Size"), _
         Key .Description = a.Field(Of String)("Description"), _
         Key .MoveQty = a.Field(Of String)("Move Qty"), _
         Key .Action = o.Field(Of String)("Action")
       }).ToList()







我刚刚添加了ToList()。而已。现在结果出现了。




I just added ToList() at the end. That's it. Now the results are showing up.


这篇关于使用LINQ加入两个数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆