在LINQ中使用参数(字符串)

时间:2015-01-29 20:52:20

标签: c# linq linq-to-sql

我试图使用listBox来选择搜索, 但我不能在LINQ中使用字符串si,我不知道如何使用它, 我制作了这段代码,但不知道如何使用si。

        var db = new DataClasses1DataContext();
        int selectedIndex = listBox1.SelectedIndex;
        string si = "";
        if (selectedIndex == 0) si = "id" ;
        if (selectedIndex == 1) si = "name";
        if (selectedIndex == 2) si = "sex";
        if (selectedIndex == 3) si = "address";
        if (selectedIndex == 4) si = "phone";
        if (selectedIndex == 5) si = "web";
        var qsearch = db.users.Where(c => c.[i want use si in this place].Contains(textBox7.Text));
        dataGridView1.DataSource = qsearch;

1 个答案:

答案 0 :(得分:0)

为什么不在每个if语句中设置查询

switch (selectedIndex)
{
   case 0:
     dataGridView1.DataSource = db.users.Where(u => u.Id.Contains(...));
     break;
   ....
 }