LINQ查询中的第二次联接不会导致返回任何记录

本文关键字:返回 记录 任何 查询 第二次 LINQ | 更新日期: 2023-09-27 18:00:46

我试图连接三个列表,但由于某种原因,第二个连接导致没有返回任何数据。我最终确实想"离开"加入这个,但我正在努力解决它没有正确加入的问题。

我的三个列表:

var equipmentAliasList = new List<EquipmentAlias> {
    new EquipmentAlias { Alias = "100-00001"},
    new EquipmentAlias { Alias = "100-00005"},
    new EquipmentAlias { Alias = "100-00004"},
    new EquipmentAlias { Alias = "100-00003"},
    new EquipmentAlias { Alias = "100-00002"},
}
var assets = new List<Asset> {
    new Asset { AssetNum = "100-00001", LineAsset = "200-00001"},
    new Asset { AssetNum = "100-00005", LineAsset = "200-00002"},
    new Asset { AssetNum = "100-00004", LineAsset = ""},
    new Asset { AssetNum = "100-00003", LineAsset = "200-00001"},
    new Asset{ AssetNum = "100-00002", LineAsset = ""},
};
var lineAssets = new List<Asset> {
    new Asset { AssetNum = " 200-00001", IsRunning = true },
    new Asset { AssetNum = " 200-00002", IsRunning = false },
};

因此,从equipmentAliasList中选择所有在assetNum上加入assets,然后在asset.LineAsset equals lineAsset.AssetNum上加入lineAssets

以下是我的想法:

(from e in equipmentAliasList
join ma in assets on e.Alias equals ma.AssetNum
join la in lineAssets on ma.LineAsset equals la.AssetNum
select new EquipmentAlias
{
    Alias = e.Alias,
    IsPor = la.IsRunning,
}).ToList();

结果应该是:

  Alias   |   IsPor
----------|-----------
100-00001 |   true
100-00005 |   false
100-00003 |   true

但是,从assetslineAssets的联接导致查询不返回任何结果。

LINQ查询中的第二次联接不会导致返回任何记录

如果您注意到lineAssets,AssetNum字段中有空格。将" 200-00001"更改为"200-00001"

var lineAssets = new List<Asset> {
    new Asset { AssetNum = "200-00001", IsRunning = true },
    new Asset { AssetNum = "200-00002", IsRunning = true },
};