Brian Var
Brian Var

Reputation: 6227

How can you filter list by property value in an int list?

I've defined two lists and now want to filter the escHistory list by Application property matching the filterRIDS RID values.

But stepping through the LINQ that filters out the escHistory I get a return count of zero. Although the list does contain records with Application property values matching those defined in filterRIDS.

Question:

How can you filter list by property value in another list?

This is a basic gist of the filter code:

List<int> filterRIDS = new List<int> { 115841, 200463 };
List<Escalation> escHistory = new List<Escalation>();

//Filter list by RID's matching
escHistory = escHistory.Where(r => r.Application.Contains(filterRIDS.ToString())).ToList<Escalation>();

Upvotes: 2

Views: 1571

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

Problem is filterRIDS.ToString() which returns name of List<int> class:

"System.Collections.Generic.List`1[System.Int32]"

Of course you don't have history objects which contain this value in Application field. If you want to check whether Application string contains any string from given list:

escHistory = escHistory
     .Where(r => filterRIDS.Any(id => r.Application.Contains(id.ToString())))
     .ToList();

Upvotes: 1

fubo
fubo

Reputation: 45947

You swapped the query

escHistory = escHistory.Where(r => filterRIDS.Contains(int.Parse(r.Application))).ToList();

Upvotes: 5

Related Questions