以下列表都包含QuoteID字段。消除currentQuotes列表中具有quoteData列表中存在的QuoteID的项目的最佳方法是什么?感谢。
//Establish - Instantiate lists
IList<QuotePaneView> currentQuotes = new List<QuotePaneView>();
IList<Quote> quoteData = new List<Quote>();
//Fill lists
currentQuotes = theQuotePaneService.GetAllQuotePanelStuff();
quoteData = theQuoteDataService.GetAllQuoteData();
答案 0 :(得分:2)
简单的Linq可以在这里提供帮助:
请考虑以下事项:
var list1 = new List<int>();
list1.Add(1);
list1.Add(3);
list1.Add(5);
var list2 = new List<int>();
list2.Add(1);
list2.Add(2);
list2.Add(3);
var diff = list1.Except(list2);
答案 1 :(得分:1)
您可以使用Except
根据ID获取设置差异。然后,您只需将结果(不在quoteData中的唯一ID)加入currentQuotes
列表。
var notInQuoteData = currentQuotes.Select(q => q.QuoteID).Except(quoteData.Select(q => q.QuoteID));
var result = (from qUnique in notInQuoteData
join q in currentQuotes on qUnique equals q.QuoteID
select q).ToList();