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

查看:86
本文介绍了C#多个Linq查询搜索问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个linq查询,用于基于每个查询的一条特定标准来搜索信息数据库.例如按ID或按名称等.

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.

例如:

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

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:

返回所有有关的问题 DepartmentName ==计算机科学与技术系ModuleTitle ==分布式系统.

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

我们将不胜感激.

这是当前代码:

//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)
...

因此,如果您搜索cond1 ="和cond2 ="somevalue",实际上,您仅在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.

这篇关于C#多个Linq查询搜索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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