在"Where"中的动态表述.条款-Linq to SQL [英] Dynamic Expressions in "Where" Clause - Linq to SQL

查看:85
本文介绍了在"Where"中的动态表述.条款-Linq to SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new in LINQ so I hope this isn't a stupid question:

我有一个表格,其中有很多内容显示在数据网格中,我希望用户能够通过使用网格上方的一些组合框来过滤网格(例如搜索栏)

I have a table with a lot of content presented in a datagrid, and I want the user to be able to filter the grid by using some combo boxes above the grid [like a search bar]

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

I created a method that takes the text in the combo boxes, and placing it the "Where" clause:

    public void find()
    {
        string disName;
        string statusName;


        disName = RMcmbDis.Text; //This Get the first string to filter
        statusName = RMcmbStatus.Text; // this get the second string to filter

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

//Here I gather all the data I need

        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".

Now I have a problem: I want the user to be able to leave one of the combo boxes empty and if he does so, this means he doesn't want to filter that criteria. [E.G - The combo "RMcmbDis" has "Math" and the Status combo ["RMcmbStatus"] is empty, so the grid will show only "Math" in all status'.

我该怎么做? 谢谢你们... N.

How Do I do that? Thanks guys... N.

推荐答案

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

You can just add Where() clauses if the condition you want is true...

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;

有关处理大量过滤器的一种选择,请参见下面的评论.另一种选择是使用辅助方法:

See comment below for one option for handling lots of filters. Another option is to use a helper method:

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

您将这样使用:

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;

这篇关于在"Where"中的动态表述.条款-Linq to SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆