创建一个取决于多个搜索字段的动态LINQ查询的最有效方法?[英] Most efficient way to create a dynamic LINQ query that depends on multiple search fields?

本文是小编为大家收集整理的关于创建一个取决于多个搜索字段的动态LINQ查询的最有效方法?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在创建一个Web应用程序,它根据输入的条件从数据库中检索数据. 问题是我有10个不同的搜索字段,只需要其中一个填充,其余的可以为空.

所以我拥有的是:

Textbox1
Textbox2
..
..
Textbox10

我的当前查询是:

checked = false;
if (Textbox1.Text != null)
{
   result = //query here
   checked = true;
}

if (Textbox2.Text != null)
{
    if(checked==false)
    {
       result = //new query here
       checked = true;
    } 
    else
    {

        result = results.Where(...new query to filter Textbox2 from previous 
        query)
    }
}

等.

如何在一个查询中构建此功能,并忽略没有值的文本框?

感谢

推荐答案

在您的问题中提到时,您只需要在每个步骤中缩小您的查询.

var result = //query here

if (Textbox1.Text != null)
{
  result = result.Where(r=> r.x == Textbox1.Text);
}

if (Textbox2.Text != null)
{
  result = result.Where(r=> r.y == Textbox2.Text);
}
...

return result;

其他推荐答案

替代方法是使用||运算符在查询本身内进行空/空检查,以及条件检查文本属性是否具有值,括号内部括起来,以形成"子子句" .因为||运算符将在一个方面评估为true时返回true,并且评估从左到右完成,始终将null/空检查.

以这种方式,每个"子子句"返回true如果文本框文本是null或空,或者它将基于该文本框的文本值返回条件的评估.有效地,此"忽略"文本框的文本属性为null或空:

var result = data.Where(d => 
    (string.IsNullOrEmpty(Textbox1.Text) || d.X == Textbox1.Text) && 
    (string.IsNullOrEmpty(Textbox2.Text) || d.Y == Textbox2.Text) && 
    (string.IsNullOrEmpty(Textbox3.Text) || d.Z == Textbox3.Text));

本文地址:https://www.itbaoku.cn/post/1557009.html

问题描述

I'm creating a web application that retrieves data from the database based on the criteria entered. The problem is that I have 10 different search fields and only one of them is required to be filled, the rest can be null.

So what I have is:

Textbox1
Textbox2
..
..
Textbox10

My current query is:

checked = false;
if (Textbox1.Text != null)
{
   result = //query here
   checked = true;
}

if (Textbox2.Text != null)
{
    if(checked==false)
    {
       result = //new query here
       checked = true;
    } 
    else
    {

        result = results.Where(...new query to filter Textbox2 from previous 
        query)
    }
}

and so on.

How can I build this in one query and ignore the textboxes that don't have values?

Thanks

推荐答案

As you mentioned in your question you only need to narrow your query on each step.

var result = //query here

if (Textbox1.Text != null)
{
  result = result.Where(r=> r.x == Textbox1.Text);
}

if (Textbox2.Text != null)
{
  result = result.Where(r=> r.y == Textbox2.Text);
}
...

return result;

其他推荐答案

An alternative approach is to do the null/empty check inside the query itself, using an || operator along with the conditional check if the text property has a value, inside parenthesis to form a "sub clause". Because the || operator will return true as soon as one side evaluates to true, and evaluation is done from left to right, always put the null/empty check first.

In this way, each "sub clause" returns true if the textbox text is null or empty, or it will returns the evaluation of the condition based on the text value of that textbox. Effectively, this "ignores" the text property for textboxes that are null or empty:

var result = data.Where(d => 
    (string.IsNullOrEmpty(Textbox1.Text) || d.X == Textbox1.Text) && 
    (string.IsNullOrEmpty(Textbox2.Text) || d.Y == Textbox2.Text) && 
    (string.IsNullOrEmpty(Textbox3.Text) || d.Z == Textbox3.Text));