mwild
mwild

Reputation: 1655

LINQ Where clause Multiple conditions

Im trying to get all the data rows that contain a certain set of data.

In my database i have some values, including start and length.

I have an array that contains integers called startTimes and another called endTimes.

I need a where clause that would return the records that have a start value contained in startTimes, OR start+length contained in endTimes.

Is there any way to do this?

Thanks

    IQueryable<request> proposedRequest = db.requests.Include(r => r.rooms);
        proposedRequest = proposedRequest.Where(r=>r.booked.Equals(1));
        proposedRequest = proposedRequest.Where(r=>r.roundID.Equals(roundID))8;
        proposedRequest = proposedRequest.Where(r=>r.day.Equals(day));
        int[] startTimes;
        int[] endTimes;
        for(var q=0;q<time;q++){
            startTimes[startTimes.Length] = time + q;
            endTimes[endTimes.Length] = time + q + 1;
        }
        proposedRequest = proposedRequest.Where(//what goes in here);

Upvotes: 3

Views: 233

Answers (3)

Donald.Record
Donald.Record

Reputation: 301

proposedRequest.Where(pr => startTimesArray.Contains(pr.start) || endTimesArray.Contains(pr.start + pr.length));

Upvotes: 1

Mackan
Mackan

Reputation: 6271

will something like this suffice?

var data = collection.Where(
        t => startTimes.Contains(t.theTime) || 
        endTimes.Contains(t.theTime + theLength));

Upvotes: 2

Dawood Awan
Dawood Awan

Reputation: 7328

i have an array startTime=[1,3,4] and an array endTime=[2,4,5] lets say. I need to see if any of those values match my records? I dont think the above would do the job

To check if you have value in Array of int, Use the Contains Method:

proposedRequest = proposedRequest.Where(s => startTimes.Contains(s.INT_ID) 
                                        || endTimes.Contains(s.INT_ID));

Syntax: ARRAY.Contains(ID_TO_SEARCH)

it returns a boolean:

var list = new List<int> {1,2,3,4,5};
var intVar = 4;
var exists = list.Contains(intVar);

Upvotes: 2

Related Questions