[c#]禁用查询内 lambda

标签: Linq Lambda C#
发布时间: 2017/2/28 21:32:09
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

它的可能的或者如何禁用查询内 lambda 表达式?

例如,如果我有一个复选框,可以禁用,如果每个查询不签入。

"如果我有一个复选框 EmployeeNumber 和未选中它,该查询将被禁用或跳过."

ViewBag.dataGWUTA = from user in db.ActiveDirectory2.Where(users =>
                //Disable if not checked
                (!String.IsNullOrEmpty(search) ? users.EmployeeNumber.Contains(search) : users.EmployeeNumber == null) ||
                //Disable if not checked
                (!String.IsNullOrEmpty(search) ? users.SamAccountName.Contains(search) : users.SamAccountName == null) ||
                //Disable if not checked
                (!String.IsNullOrEmpty(search) ? users.GivenName.Contains(search) : users.GivenName == null) ||
                //Disable if not checked
                (!String.IsNullOrEmpty(search) ? users.Surname.Contains(search) : users.Surname == null) ||
                //Disable if not checked
                (!String.IsNullOrEmpty(search) ? users.EmailAddress.Contains(search) : users.EmailAddress == null) ||
                //Disable if not checked
                (!String.IsNullOrEmpty(search) ? users.Guid.Contains(search) : users.Guid == null) ||
                //Default
                (!String.IsNullOrEmpty(search) ? users.SID.Contains(search) : users.SID == null))

                                where user.DateCreated >= created && user.DateModified <= modified
                                select user;

我试着用 preprocessor directives 但哈哈很无用我的复选框,因为您需要定义顶部的条件 using directives

举个例子

#define EmployeeNumber
using System;
...
#if EmployeeNumber
                //Disable if not checked
                (!String.IsNullOrEmpty(search) ? users.EmployeeNumber.Contains(search) : users.EmployeeNumber == null) ||
#elif SamAccountName
....

更新︰

我们可以做这样的-"但我不知道什么应该放 else(:) "

(EmployeeNumber.Checked ? (!String.IsNullOrEmpty(search) ? users.EmployeeNumber.Contains(search) : users.EmployeeNumber == null) : /*What should I put here???*/) ||

解决方法 1:

好的疯狂的巫师答案是

(!checkBox1.Checked || (!String.IsNullOrEmpty(search) ? users.EmployeeNumber.Contains(search) : users.EmployeeNumber == null)) || ...

但问题是什么时候 checkBox.Checked 处于未选中状态或条件为 true (!false) ||.. 查询将停止或它不会持续到其他条件。

但由于他的回答,我找到了解决办法︰

因此,允许条件下继续向其他条件它必须总是考虑为 false。

(Bool ? (?:) : false) || ...

在这种情况下条件将一直继续。

看到我完整的解决方案︰

ViewBag.dataGWUTA = from user in db.ActiveDirectory2.Where(users =>
            //Disable if not checked
            (checkBox1.Checked ? (!String.IsNullOrEmpty(search) ? users.EmployeeNumber.Contains(search) : users.EmployeeNumber == null) : false) ||
            //Disable if not checked
            (checkBox1.Checked ? (!String.IsNullOrEmpty(search) ? users.SamAccountName.Contains(search) : users.SamAccountName == null) : false) ||
            //Disable if not checked
            (checkBox1.Checked ? (!String.IsNullOrEmpty(search) ? users.GivenName.Contains(search) : users.GivenName == null) : false) ||
            //Disable if not checked
            (checkBox1.Checked ? (!String.IsNullOrEmpty(search) ? users.Surname.Contains(search) : users.Surname == null) : false) ||
            //Disable if not checked
            (checkBox1.Checked ? (!String.IsNullOrEmpty(search) ? users.EmailAddress.Contains(search) : users.EmailAddress == null) : false) ||
            //Disable if not checked
            (checkBox1.Checked ? (!String.IsNullOrEmpty(search) ? users.Guid.Contains(search) : users.Guid == null) : false) ||
            //Default
            (checkBox1.Checked ? (!String.IsNullOrEmpty(search) ? users.SID.Contains(search) : users.SID == null) : false))

                            where user.DateCreated >= created && user.DateModified <= modified
                            select user;
官方微信
官方QQ群
31647020