タイプ A のオブジェクトの 1 つのコレクションから Guid を抽出して、タイプ B のオブジェクトの別のコレクションからこれらの Guid を除外するために、LINQ をどのように実装しますか。オブジェクト A とオブジェクト B の両方に、「ID」と呼ばれる Guid フィールドがあります。
私は次のものを持っています:
ObservableCollection<Component> component
コンポーネントには次ID
のタイプのフィールドがありますGuid
ObservableCollection<ComponentInformation> ComponentInformationCollection
ComponentInformation には次ID
のタイプのフィールドがありますGuid
私の実装:
component =>
{
if (component != null)
{
var cancelledComponents = new List<ComponentInformation>();
foreach (Component comp in component)
{
cancelledComponents.Add(new ComponentInformation() { ID = comp.ID });
}
this.ComponentInformationCollection.Remove(cancelledComponents);
}
});
私が解決しようとしてきたより洗練された解決策があると思いますが、私が遭遇し続ける問題は、タイプがエラーを出さないように「新しいComponentInformation」を作成することです。
====== 最終的な解決策 =======
var cancelledComponentIDs = new HashSet<Guid>(component.Select(x => x.ID));
this.ComponentInformationCollection.Remove(
this.ComponentInformationCollection.Where(x => cancelledComponentIDs.Contains(x.ID)).ToList());
ありがとう: Jason - これを最終的な解決策のテンプレートとして使用しました (以下にリスト)。Servy - 比較子を使用することもできましたが、この特定のシナリオでは、1 回限りの使用タイプの状況のため、比較子は必要ではなかったと思います。
ComponentInformationCollection は、変更されたときに INotifyChangedEvent (MVVM パターン) をトリガーする Silverlight DependencyProperty であるため、上記のソリューションが私の状況に最適でした。