Where子句针对多个选项

时间:2012-06-08 17:18:22

标签: c# asp.net entity-framework listbox linq-to-entities

Linq to entities语法中是否有“IN”关键字?我有一个列表框,其中添加了多个项目。我想在db中搜索这些项目。我的查询如下:

 var result = context.data_vault
                      .Where(d => d.STATE == lstStates.SelectedItem.Text).OrderBy(d=>d.dv_id)
                      .Skip(e.NewPageIndex * GridView1.PageSize)
                      .Take(GridView1.PageSize)
                      .ToList();

我想要d => d.STATE == lstStates.Items。如何改变?

3 个答案:

答案 0 :(得分:1)

代码:

string[] arr = listview.Items
                       .Cast<ListItem>()
                       .Select(i => i.Text) // or .Value
                       .ToArray();

db.Where(d => arr.Contains(d.STATE);

应该被翻译成查询:

... WHERE STATE IN ('your', 'array', 'items')

答案 1 :(得分:0)

试试这个:

var result = context.data_vault
                      .Where(d => lstStates.Items.Contains(d.STATE)).OrderBy(d=>d.dv_id)
                      .Skip(e.NewPageIndex * GridView1.PageSize)
                      .Take(GridView1.PageSize)
                      .ToList();

在这里,

lstStates is List<string>

答案 2 :(得分:0)

您可以尝试.Any().Contains()语法

这样的例子:

select d from context.Data
Where ListofIdsFromUI.Any(d.Id)
select d