Joe Ruder
Joe Ruder

Reputation: 2194

use linq to get a sub list that contains elements from an array

I have tried various version of this but I am just off somewhere. I am using Teleric Raddropdowncheckedlist

var states = stateDropDownList.CheckedItems.ToList();
var filteredStops = (from stop in aDb.Stop_address_details 
                     where states.Contains(stop.Stop_state) select stop).ToList();

States contains a array of IL, AL etc. Here is the sample data from states - in the debugger it says states count 3, it has 3 elements 0=AZ, 1=IL and 2=AL.

stop_address_details contains the field stop_state.

I just need records where the stop_state is included in states.

I am not getting a result because it wont build - error: The number of parameters of this lamda expression does not match the number of paramenters of delegate.

Solution:

var states = stateDropDownList.CheckedItems.Select(i => i.Value.ToString()).ToList();
        var filteredStops = (from stop in aDb.Stop_address_details where states.Contains(stop.Stop_state) select stop).ToList();

The original states was returning a array of items, not strings. Thank you for the help. Joe

Upvotes: 0

Views: 629

Answers (1)

Mihai Caracostea
Mihai Caracostea

Reputation: 8466

Try using something like this:

var states = stateDropDownList.CheckedItems.Select(i=>i.Value.ToString()).ToList();

That i.Value bit is very dependent on what DropDownList you are using. And is not guaranteed to work if CheckedItems is a custom collection type which doesn't implement standard interfaces, either.

Upvotes: 1

Related Questions