[英]How to Compare two Generic List one contain int and another contain class object
如何比較這兩個通用列表並按ID獲取匹配數據
List<int> _GetRecCust;
List<Tranobject> GetTransDeta;
我試過了
var TransactionsToProcess = GetTransDeta.Where(x => _GetRecCust.Contains(Convert.ToInt32(x.ID)));
但無法獲取命令ID
數據。
兩者都具有ID
作為字段。
提前致謝
最有效的方法是使用Enumerable.Join
:
var common = from x in GetTransDeta
join id in _GetRecCust
on int.Parse(x.Id) equals id
select id;
List<int> commonIDs = common.ToList();
順便說一句,為什么Tranobject.Id
不是字符串?
編輯 :由於您已經評論了存在null
,因此可以首先使用IsNullOrWhiteSpace
:
var common = from x in GetTransDeta
where !string.IsNullOrWhiteSpace(x.Id)
join id in _GetRecCust
on int.Parse(x.Id) equals id
select id;
嘗試這個:
GetTransData.Where(gtd => _GetRecCust.Any(grc => grc == int.Parse(gtd.Id))).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.