Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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到SQL_C#_Linq To Sql_Where Clause - Fatal编程技术网

C# “中的动态表达式”;其中;子句-Linq到SQL

C# “中的动态表达式”;其中;子句-Linq到SQL,c#,linq-to-sql,where-clause,C#,Linq To Sql,Where Clause,我是LINQ的新手,所以我希望这不是一个愚蠢的问题: 我在datagrid中有一个包含大量内容的表,我希望用户能够通过使用网格上方的一些组合框(如搜索栏)来过滤网格 我创建了一个方法,将文本放入组合框中,并将其放在“Where”子句中: //这里我收集了我需要的所有数据 var allCNT = from x in cntDB.releases join dis in cntDB.disciplines on x.discipline e

我是LINQ的新手,所以我希望这不是一个愚蠢的问题:

我在datagrid中有一个包含大量内容的表,我希望用户能够通过使用网格上方的一些组合框(如搜索栏)来过滤网格

我创建了一个方法,将文本放入组合框中,并将其放在“Where”子句中:

//这里我收集了我需要的所有数据

        var allCNT = from x in cntDB.releases
                     join dis in cntDB.disciplines on x.discipline equals dis.discipline_id
                     join btch in cntDB.batches on x.batch_num equals btch.batch_id
                     join z in cntDB.status on x.status equals z.status_id

                     select new { dis.discipline_name, x.grade, x.batch_num, btch.batch_name, z.status_description, x.segment_leader, x.ped_leader, x.release_location, x.comments, x.QA_Verdict };
//这里我做过滤

        var find = allCNT.Where(a => a.discipline_name == disName && a.status_description == statusName);


        dataGridView1.DataSource = find;
    }
现在我有一个问题:我希望用户能够将其中一个组合框留空,如果他这样做,这意味着他不想过滤该条件。[例如-组合“RMcmbDis”有“Math”,而状态组合[“RMcmbStatus”]为空,因此网格在所有状态中仅显示“Math”

我该怎么做? 谢谢大家。。。
N.

如果所需条件为真,则只需添加
Where()
子句即可

var results = allCNT;

if (!string.IsNullOrEmpty(disName))
    results = result.Where(a => a.discipline_name == disname);

if (!string.IsNullOrEmpty(statusName))
    results = results.Where(a => a.status_description == statusName);

dataGridView1.DataSource = results;
有关处理大量筛选器的一个选项,请参见下面的注释。另一个选项是使用帮助器方法:

T AddFilter<T>(IQueryable<T> results, string filterValue, Expression<Func<T, bool>> predicate)
{
    if(!string.IsNullOrEmpty(filterValue))
        return results.Where(predicate);
    return results;
}

根据您的标准,您可以添加多个
Where
子句,例如:

var find = allCNT;
if (!string.IsNullOrEmpty(disName))
{
    find = find.Where(a => a.discipline_name == disName);
}
if (!string.IsNullOrEmpty(statusName))
{
    find = find.Where(a.status_description == statusName);
}

非常感谢!!但是如果有8个组合框呢?难道没有一种方法可以做到像:a=>a.status\u description==everything&&a.disprince\u name===specific*?一个选项是构建一个大的
Where()
子句,看起来像这样:
Where(a=>(disname==“”| a.trictional\u name==disname)&(statusName==)| | a.status_description==statusName)和&…)
var results = allCNT;
results = AddFilter(results, disname, a => a.discipline_name == disname);
results = AddFilter(results, statusName, a => a.status_description == statusName);
results = AddFilter(results, whatever, a => a.whatever == whatever);
// ...
dataGridView1.DataSource = results;
var find = allCNT;
if (!string.IsNullOrEmpty(disName))
{
    find = find.Where(a => a.discipline_name == disName);
}
if (!string.IsNullOrEmpty(statusName))
{
    find = find.Where(a.status_description == statusName);
}