简体   繁体   English

使用linq获取包含数组元素的子列表

[英]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 我正在使用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. 状态包含一个IL,AL等数组。这是来自状态的样本数据-在调试器中它说状态计数为3,它具有3个元素0 = AZ,1 = IL和2 = AL。

stop_address_details contains the field stop_state. stop_address_details包含字段stop_state。

I just need records where the stop_state is included in states. 我只需要在状态中包含stop_state的记录。

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. 我没有得到结果,因为它不会生成-错误:此lamda表达式的参数数量与委托的参数数量不匹配。

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. i.Value位很大程度上取决于您使用的DropDownList。 And is not guaranteed to work if CheckedItems is a custom collection type which doesn't implement standard interfaces, either. 如果CheckedItems是也不实现标准接口的自定义集合类型,则也不保证能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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