Bill
Bill

Reputation: 1205

Using linq to check property against one of multiple possible values

Using linq to check property against one of multiple possible values. In this case when the status is 2 or 3? Can this be done without an or operator?

var x = (from b in books
         where b.statusCode.Contains(2, 3))
         select new ...

Upvotes: 0

Views: 827

Answers (2)

romfir
romfir

Reputation: 402

It can be done like this (assuming that statusCode is int)

var values = new int[] { 2, 3 };

var x = (from b in books
     where values.Contains(b.statusCode))
     select new ...

or You can try to inline it like that:

    var x = (from b in books
     where (new int[] { 2, 3 }.Contains(b.statusCode)))
     select new ...

Upvotes: 1

Nik FP
Nik FP

Reputation: 3093

You could set up the list of values as a List<int>, call it something like ValueList, and then on your where line:

where ValueList.Contains(b.statusCode)

This should compare the statusCode against all list values and return the records that match, and you get the benefit of a dynamic list that can be reset with different values to return collections of other status codes.

Upvotes: 1

Related Questions