简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM