Amit
Amit

Reputation: 3478

LINQ Contains clause - more then one value in criteria

I have a collection which is filtered based on criteria for which I am using contains clause given below. I am further creating a new collection based on filtered records and returning the same.

Issue:

If I have more then one value in "multipleCodes" which is used in contains clause.. it does not work. If I put only 1 value... it does work.

Any idea what am I missing ? or is there any better approach to filter records and retrun only filtered resultset ?

collectionToFilter.Where(d => (d.Code.**Contains**(multipleCodes)) &&
                d.NeededDate > minNeedDate && d.NeededDate < maxNeedDate)
                    .ToList()
                    .ForEach(d => filteredCollection.Add(d));

Upvotes: 0

Views: 45

Answers (2)

Robert Petz
Robert Petz

Reputation: 2764

From your comment I wanted to show you a cleaner and more readable answer.

var _multipleCodes = multipleCodes.Split(',');
collectionToFilter.Where(d => _multipleCodes.Contains(d.Code));

Upvotes: 1

James Curran
James Curran

Reputation: 103545

collectionToFilter.Where(d => (multipleCodes.Any(mc=>d.Code.Contains(mc))) &&

Upvotes: 2

Related Questions