Question

How to compare these two Generic list and get the matching data as per ID

List<int> _GetRecCust;
List<Tranobject> GetTransDeta;

I tried

var TransactionsToProcess = GetTransDeta.Where(x => _GetRecCust.Contains(Convert.ToInt32(x.ID)));

but not able to get a comman ID data.

Both having ID as a field.

Thanks in advance

Was it helpful?

Solution

The most efficient approach is using 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();

By the way, why is Tranobject.Id a string at all?

Edit: Since you have commented that there are nulls, you could use IsNullOrWhiteSpace first:

var common = from x in GetTransDeta
             where !string.IsNullOrWhiteSpace(x.Id)
             join id in _GetRecCust
             on int.Parse(x.Id) equals id
             select id;  

OTHER TIPS

Try this:

GetTransData.Where(gtd => _GetRecCust.Any(grc => grc == int.Parse(gtd.Id))).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top