C#多重LINQ查询搜索问题[英] C# Multiple Linq Query Search Issue

本文是小编为大家收集整理的关于C#多重LINQ查询搜索问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有多个LINQ查询,用于根据每个查询的单个特定条件搜索信息数据库.例如通过ID或名称等.

当前,用户可以仅使用一种查询方法搜索.问题是我希望用户能够使用多个标准搜索,而不必编写新的查询来结合来自多个查询的标准.

例如:

下面我有一个查询,该查询根据存储在下面的部门名称中返回一组问题,另一个查询根据模块标题返回一组问题,这些问题存储在下面. 由于当前用户只能按部门名称或模块标题进行搜索,例如计算机科学或分布式系统,我想更改此操作,以便用户可以指定以下内容:

返回所有的问题 DepartmentName ==计算机科学&& moduletitle ==分布式系统.

帮助您将不胜感激.

这是当前代码:

//Department Name Query
public static IQueryable SearchByDepartmentNameInfo(string deptName)
    {

        ExamineDataContext dc = new ExamineDataContext();

        var queryResult = from q in dc.GetTable<Question>()
                          where q.Topic.Module.Department.DepartmentName.Equals(deptName)
                          join s in dc.Solutions
                          on q.QuestionID equals s.QuestionID
                          into qs // note grouping        
                          select new
                          {
                              Module = q.Topic.ModuleTitle,
                              Topic = q.TopicName,
                              Question = q.QuestionText,
                              QuestionType = q.QuestionType,
                          };
        return queryResult;
    }

    //Module Title Query
    public static IQueryable SearchByModuleTitleInfo(string modTitle)
    {

        ExamineDataContext dc = new ExamineDataContext();

        var queryResult = from q in dc.GetTable<Question>()
                          where q.Topic.Module.ModuleTitle.Equals(modTitle)
                          join s in dc.Solutions
                          on q.QuestionID equals s.QuestionID
                          into qs // note grouping        
                          select new
                          {
                              Module = q.Topic.ModuleTitle,
                              Topic = q.TopicName,
                              Question = q.QuestionText,
                              QuestionType = q.QuestionType,
                          };
        return queryResult;
    }

推荐答案

您可以将它们组合成单个查询,只有在设置的情况下应用条件:

where (cond1 == "" || row.field1 == cond1)
&& (cond2 == "" || row.field2 == cond2)
...

因此,如果您搜索cond1 =""和cond2 =" someValue",实际上您只在cond2上搜索.但是,如果两者都指定,它将选择两种条件的交集.

添加一些检查以确保指定至少一个条件.

其他推荐答案

您是否考虑过使功能接受iQueryable作为参数,而不是对其中的可获取效果进行硬编码?这将使您可以通过将结果从一个函数传递到另一个功能的输入来组合查询.当然,在评估最终结果之前,查询仍然不会执行.

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

问题描述

I have multiple linq queries for searching a database of information based on a single specific piece of criteria per query. e.g. by ID or by Name etc.

Currently, the user can search by use of only one query method. The problem is that I want the user to be able to search using multiple criteria, without having to write new queries that combine the criteria from multiple queries.

For Example:

Below I have a query that returns a set of questions based on the department name that they are stored under, and another query that returns a set of questions based on the module title that the questions are stored under. Since currently the user can only search by department name or by module title - e.g. Computer Science or Distributed Systems, I would like to change this so that the user can specify something like:

Return all questions that are of DepartmentName == Computer Science && ModuleTitle == Distributed Systems.

Help would be greatly appreciated.

Here is the current code:

//Department Name Query
public static IQueryable SearchByDepartmentNameInfo(string deptName)
    {

        ExamineDataContext dc = new ExamineDataContext();

        var queryResult = from q in dc.GetTable<Question>()
                          where q.Topic.Module.Department.DepartmentName.Equals(deptName)
                          join s in dc.Solutions
                          on q.QuestionID equals s.QuestionID
                          into qs // note grouping        
                          select new
                          {
                              Module = q.Topic.ModuleTitle,
                              Topic = q.TopicName,
                              Question = q.QuestionText,
                              QuestionType = q.QuestionType,
                          };
        return queryResult;
    }

    //Module Title Query
    public static IQueryable SearchByModuleTitleInfo(string modTitle)
    {

        ExamineDataContext dc = new ExamineDataContext();

        var queryResult = from q in dc.GetTable<Question>()
                          where q.Topic.Module.ModuleTitle.Equals(modTitle)
                          join s in dc.Solutions
                          on q.QuestionID equals s.QuestionID
                          into qs // note grouping        
                          select new
                          {
                              Module = q.Topic.ModuleTitle,
                              Topic = q.TopicName,
                              Question = q.QuestionText,
                              QuestionType = q.QuestionType,
                          };
        return queryResult;
    }

推荐答案

You can combine them into a single query, by only applying the condition if it's set:

where (cond1 == "" || row.field1 == cond1)
&& (cond2 == "" || row.field2 == cond2)
...

So if you search for cond1="" and cond2="somevalue", in effect you're only searching on cond2. But if you specify both, it will select the intersection of both conditions.

Add some checks to make sure at least one condition is specified.

其他推荐答案

Have you considered making the functions accept an IQueryable as a parameter instead of hard-coding the GetTable within them? This would allow you to combine queries by passing the result from one function to the input of another. Of course the query still doesn't execute until you evaluate the final result.