0
 var devSum = repository.Devices
            .Where(dev => dev.Id == deviceId)
            .SingleOrDefault();
        vmDeviceSummary result = new vmDeviceSummary
        {
          DeviceId = deviceId,

          DeviceName = devSum.Name,
          MacAddress = devSum.MacAddress,
          DeviceType = devSum.DeviceType.Name,
          Enabled = devSum.Enabled.ToString(),
          ConfigurationLoaded = devSum.ConfigurationLoaded.ToString(),
          AllowReload = devSum.AllowDataReload.ToString(),
          DataGroup = devSum.DataGroup.Name,
          ManagementGroup = devSum.ManagementGroup.Name,
        };

我认为这应该可以工作,但是如果指向 datagoups 或 managementGroup 实体的链接中的外键中有空值,则会引发错误。

我该如何解决这个问题,使它像一个外部连接并返回 null?

4

3 回答 3

1

它可能会有点难看,但你可以使用三元运算符来做到这一点:

DeviceType = devSum.DeviceType != null ? devSum.DeviceType.Name : null,
于 2013-03-07T10:43:22.070 回答
0

用这个改变上面的代码

var devSum = repository.Devices .Where(dev => dev.Id == deviceId) .FirstOrDefault();
于 2013-03-07T10:55:51.903 回答
0

你可能需要做一个Include

var devSum = repository.Devices
            .Include("DeviceType")
            .Include("ManagementGroup")
            .Where(dev => dev.Id == deviceId)
            .SingleOrDefault();
于 2013-03-07T11:01:07.340 回答