C# 具有null LINQ异常和GUID的左外部联接

C# 具有null LINQ异常和GUID的左外部联接,c#,linq,entity-framework,guid,C#,Linq,Entity Framework,Guid,我试图在一个列表上的linq中使用null进行左外连接,因此如果我有一个带有{1,2,3,4}的列表和另一个带有{1,2,3,5}的列表,我想要{4} IEnumerable<AlertChangeSet> listToClear = from a in AlertsCached join b in loadedAlerts on a.AlertId equals b.AlertId

我试图在一个列表上的linq中使用null进行左外连接,因此如果我有一个带有{1,2,3,4}的列表和另一个带有{1,2,3,5}的列表,我想要{4}

IEnumerable<AlertChangeSet> listToClear = from a in AlertsCached
                                                  join b in loadedAlerts on a.AlertId equals b.AlertId into c
                                                  from b in c.DefaultIfEmpty()
                                                  select new AlertChangeSet()
                                                       {
                                                           AlertId = b.AlertId == Guid.Empty ? a.AlertId : Guid.Empty

                                                       };
  if (listToClear.Any())
        {
            foreach (AlertChangeSet alertChangeSet in listToClear)
            {
                Guid a = alertChangeSet.AlertId;
                //SystemMonitoringService.ClearAlertAsync(alertChangeSet.AlertId.ToString(), null);
            }
        }

IEnumerable listToClear=来自AlertsCached中的
将b加入a上的loadedAlerts中。AlertId等于b。AlertId加入c
来自c.DefaultIfEmpty()中的b
选择新的AlertChangeSet()
{
AlertId=b.AlertId==Guid.Empty?a.AlertId:Guid.Empty
};
if(listToClear.Any())
{
foreach(列表目录中的AlertChangeSet AlertChangeSet)
{
Guid a=alertChangeSet.AlertId;
//SystemMonitoringService.ClearAlertAsync(alertChangeSet.AlertId.ToString(),null);
}
}
运行此代码时,会出现以下异常:

试验方法 Tgw.Systems.Alerting.Server.Test.ConfigurationTests.UpdateCacheith2RecordssameidWorking 引发的异常:System.NullReferenceException:对象引用不正确 设置为对象的实例。在 Tgw.Wcs.Alerting.MonitoringAddIn.Oms.Wcf.OmsWcfSystemMonitor.b_uc(f_u匿名类型0
2
h_u透明标识符2,警报更改集b)位于
OmsWcfSystemMonitor.cs:位于的第255行
System.Linq.Enumerable.d_31
3.MoveNext()位于 Tgw.Wcs.Alerting.MonitoringAddIn.Oms.Wcf.OmsWcfSystemMonitor.UpdateAlertsFromCache(IList`1 OmsWcfSystemMonitor.cs中的loadedAlerts):第275行 Tgw.Systems.Alerting.Server.Test.ConfigurationTests.UpdateCacheith2RecordssameidWorking() 在ConfigurationTests.ServerCoreTests.cs中:第243行

我认为问题在于Guid

试试看

AlertId = b.AlertId ?? a.AlertId ?? Guid.Empty;
因为b可以是
null
,所以不能将其与Guid.Empty进行比较

??
是空合并运算符。这意味着语句将为赋值使用第一个NOTNULL值

//编辑

你说得对。我没有测试它

AlertId = ( b == null ) ? a.AlertId : Guid.Empty;

这应该行得通。Guid是一种特殊情况,因为它在设计上不能为null。

Guid不能为null,也不能转换为Guid?因此,我得到一个编译错误:“运算符“??”不能应用于“System.Guid”类型的操作数”