Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在LINQ中使用参数(字符串)_C#_Linq_Linq To Sql - Fatal编程技术网

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

C# 在LINQ中使用参数(字符串),c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我也在尝试使用列表框来选择搜索, 但是我不能在LINQ中使用字符串si,我不知道如何使用它, 我编写了这个代码,但不知道如何使用si var db = new DataClasses1DataContext(); int selectedIndex = listBox1.SelectedIndex; string si = ""; if (selectedIndex == 0) si = "id" ; if (sel

我也在尝试使用列表框来选择搜索, 但是我不能在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;

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

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

如果查询的逻辑稍微改变,您可能会考虑使用(可用的)什么?现在,您需要在6个不同的位置更改它,并冒着为其中一个位置出错的风险,或者发生复制粘贴错误并更改错误行的属性等。这样做是无效的。Thanx,但当我尝试在开关中使用“.Contains(…)”时,这是不可用的。“…”只是“在此处插入代码”的约定。您应该将“…”替换为
textBox7.Text
。我知道,但我不能使用此代码插入开关“.Contains(textBox7.Text)”它是未知的“未知”是什么。是因为
Id
是非字符串吗?如果是这样,请尝试使用
ToString().Contains(…)
ToString().StartsWith(…)