0

リストに対して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);
            }
        }

このコードを実行すると、次の例外が発生します。

テスト メソッド Tgw.Systems.Alerting.Server.Test.ConfigurationTests.UpdateCacheWith2recordsSameIdWorking が例外をスローしました: System.NullReferenceException: オブジェクト参照がオブジェクトのインスタンスに設定されていません。OmsWcfSystemMonitor2 <>h__TransparentIdentifier2, AlertChangeSet b) in OmsWcfSystemMonitor.cs: line 255 at System.Linq.Enumerable.<SelectManyIterator>d__31の Tgw.Wcs.Alerting.MonitoringAddIn.Oms.Wcf.OmsWcfSystemMonitor.UpdateAlertsFromCache(IList`1 loadedAlerts) でcs: ConfigurationTests.ServerCoreTests.cs の Tgw.Systems.Alerting.Server.Test.ConfigurationTests.UpdateCacheWith2recordsSameIdWorking() の 275 行目: 243 行目

問題はガイドだと思います!

4

1 に答える 1

2

試す

AlertId = b.AlertId ?? a.AlertId ?? Guid.Empty;

bはnull、Guid.Emptyと比較できない可能性があるためです。

??null合体演算子です。これは、ステートメントが割り当てに最初のnull以外の値を使用することを意味します。

//編集

あなたが正しいです。私はそれをテストしませんでした。

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

これはここで機能するはずです。Guidは、設計上nullにできないため、特殊なケースでした。

于 2012-12-18T13:32:05.610 に答える