我有两个数据表:
- 数据表表1->从Excel文件导入数据
样本数据:
Account, Name, Address, Phone
A05911, Test1, LA, 1234
A05912, Test2, NY, 1235
A05912, Test2, NY, 1235
A05913, Test3, BO, 1239
- 数据表table2——从SQL数据库导入数据
样本数据:
Account, Dummy
A05911, yyyy1
A05912, xxxx2
A05913, zzzz3
我想加入这两个数据表,这样得到的数据表将是:
Account, Dummy, Name, Address, Phone
A05911, yyyy1, Test1, LA, 1234
A05912, xxxx2, Test2, NY, 1235
A05912, xxxx3, Test2, NY, 1235
A05913, zzzz4, Test3, BO, 1239
我尝试了以下查询:
var result = ( from a in table1.AsEnumerable(),
join b in table2.AsEnumberable()
on a.Field<string>("Account") equals b.Field<string>("Account")
into temp from res in temp.DefaultIfEmpty()
select new
{
Account = a.Field<string>("Account"),
Test = res == null ? null : res.Field<string>("Dummy")
});
但此查询并没有提供表1中的所有列和表2中的"Dummy"列。它只返回表1中的"Account"和表2中的"Dummy"。
我如何实现这一点,以及如何将查询结果存储到数据表中?
我想要数据表中的以下结果:
Account, Dummy, Name, Address, Phone
A05911, yyyy1, Test1, LA, 1234
A05912, xxxx2, Test2, NY, 1235
A05912, xxxx3, Test2, NY, 1235
A05913, zzzz4, Test3, BO, 1239
您需要将它们添加到select
:
select new
{
Account = a.Field<string>("Account"),
Name = a.Field<string>("Name"),
Address = a.Field<string>("Address"),
Phone = a.Field<string>("Phone"),
Test = res == null ? null : res.Field<string>("Dummy")
});