我有两个数据表:T1和T2
T1和T2都有一个列注册,但T2没有与T1相同的行。
我必须将两个表组合起来,如果注册号相同,则从T2获取列,如果不显示空白,但我需要从T1开始的所有行,(如果匹配或不匹配)。
我试过这个,但我只得到匹配的行:
var results = from table1 in T1
join table2 in T2
on (String)table1["Registration"] equals (String)table2["Registration"]
select new
{
Registration = (String)table1["Registration"],
DistanceInKM = (decimal)table1["DistanceInKM"],
TotalDistanceTravelledKM = (Double)table2["TotalDistanceTravelledKM"]
};
答案 0 :(得分:1)
您正在执行内部联接,该联接仅显示两侧都存在的行。
尝试改为使用left outer join:
var results = (from table1 in T1.AsEnumerable()
join tmp in T2.AsEnumerable() on table1["Registration"] equals tmp["Registration"] into grp
from table2 in grp.DefaultIfEmpty()
select new
{
Registration = (String)table1["Registration"],
DistanceInKM = (decimal)table1["DistanceInKM"],
TotalDistanceTravelledKM = (table2 == null ? (double?)null : (Double)table2["TotalDistanceTravelledKM"])
};