在过去的几天里,经过彻底的谷歌搜索后,我一直在努力解决这个问题,并且还没有成功地完成这项工作。我是LINQ和C#的新手。基本上,我试图连接两个表,所有一个表,但我尝试加入的列确实包含空值。然后将另一个表的子集连接到该列。
我想要发生的是,在加入后,我会显示第一个表中显示的所有信息,而不是我当前只显示未显示的列的信息。
我知道这是一个特别优雅的解释,所以我将发布一些简化的puesdo代码:
tblAsset tblLookUps
AssetTag int Domain String
Type int DomainID int
Model int Description String
因此,表格中的信息将类似于:
100, 1, 1 TYPE, 1, PC
101, 1, null TYPE, 2, Monitor
102, 1, 2 MODEL, 1, Old PC
103, 2, null MODEL, 2, New PC
104, 2, null MODEL, 3, Old Monitor
105, 2, 3 MODEL, 4, New Monitor
那么我想要LINQ查询给我的是这样的
AssetTag Type TypeDescription Model ModelDescription
100 1 PC 1 Old PC
101 1 PC null null
102 1 PC 2 New PC
103 2 Monitor null null
104 2 Monitor null null
105 2 Monitor 3 Old Monitor
然而,目前LINQ返回了这个:
AssetTag Type TypeDescription Model ModelDescription
100 1 PC 1 Old PC
102 1 PC 2 New PC
105 2 Monitor 3 Old Monitor
很明显,当尝试加入时,如果值为null则会被遗漏,这当然是我理解的,但是我并不关心它是否为null所以非常希望能够看到它!
我目前的LINQ看起来像这样:
var AllAssets = from assets in dContext.tblAssets
join type in dContext.tblLookups.Where(x => x.Domain == "ASTYPE")
on assets.Type equals type.DomainID
join model in dContext.tblLookups.Where(x => x.Domain == "MODEL")
on assets.Model equals model.DomainID
select new Asset
{
AssetTag = assets.AssetTag
TypeID = assets.Type
TypeDescription = type.Description
ModelID = assets.Model
ModelDescription = model.Description
}
return AllAssets;
我试过摆弄.DefaultIfEmpty()和其他一些东西,但我还没有设法解决它,感觉我已经达到了功能的终点,任何提示或指针都会非常棒!
答案 0 :(得分:0)
var q = from a in tblAssets
join l in tblLookUps on a.Model equals l.DomainID
into tblAssettblLookUps
from l in tblAssettblLookUps.DefaultIfEmpty()
select new
{
AssetTag = a.AssetTag,
Type = a.Type,
Model = a.Model
ModelDescription = l.Description
};
尝试这样的查询。