LINQ使用NULL元素加入多个数据集

时间:2016-09-21 10:59:48

标签: c# linq

的数据集

心跳

Computer      IP
__________________________
PC 1          192.168.2.1
PC 2          192.168.2.2
PC 3          192.168.2.3


保护

Computer      Protection Status
__________________________
PC 1          Protected
PC 3          Not Protected


效果

Computer      CPU Percentage
__________________________
PC 1          52%
PC 3          23%


更新

Computer      Updates
__________________________
PC 2          206
PC 3          127


我想要的是什么:

对返回以下输出的数据集执行LINQ查询:

Computer      IP             Protection Status     CPU Percentage   Updates
____________________________________________________________________________
PC 1          192.168.2.1    Protected             52%
PC 2          192.168.2.2                                           206
PC 3          192.168.2.3    Not Protected         23%              127


我尝试了什么:

var joined = from heartbeat in heartbeats
             join protection in protections on heartbeat.Computer equals protection.Computer into protectionJoined
             from p in protectionJoined.DefaultIfEmpty()
             join perf in performance on p.Computer equals perf.Computer into performaceJoined
             from x in performaceJoined.DefaultIfEmpty()
             join update in updates on x.Computer equals update.Computer into final
             from e in final.DefaultIfEmpty()
             select new ServerOverview
             {
                 ComputerName = heartbeat.Computer,
                 ComputerIP = heartbeat.ComputerIP,
                 ProtectionStatus = p == null ? null : p.ProtectionStatus,
                 CPUPercent = x == null ? -1 : x.CPUPercent,
                 Updates = e == null ? -1 : e.Updates
             };

但这缺少了返回缺少列的行:

Computer      IP             Protection Status     CPU Percentage   Updates
____________________________________________________________________________
PC 3          192.168.2.3    Not Protected         23%              127

1 个答案:

答案 0 :(得分:1)

此代码将返回您想要的内容。假设计算机名称不在任何数据集中重复,即它是唯一标识符。

thread C

这将返回以下集合:

enter image description here