Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq上的where all_C#_Asp.net Mvc_Linq - Fatal编程技术网

C# 使用linq上的where all

C# 使用linq上的where all,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我正在尝试为我的应用程序实现一个搜索功能,但是如果没有指定关键字并且我有一个where子句,我想发送所有列表 以下是我的行动: Public ActionResult classes(string keyword ="") { EmployeeContext emp = new EmployeeContext(); List<classlist> asd = (from subj in emp.Subjects

我正在尝试为我的应用程序实现一个搜索功能,但是如果没有指定关键字并且我有一个
where
子句,我想发送所有列表

以下是我的行动:

Public ActionResult classes(string keyword ="")
{
        EmployeeContext emp = new EmployeeContext();

        List<classlist> asd = (from subj in emp.Subjects
                               join prof in emp.professors on subj.id equals prof.id
                               join dep in emp.departments on prof.id equals dep.id
                               where subj.subj == keyword
                               select new classlist()
                               {
                                   id = subj.id,
                                   subj = subj.subj,
                                   days = subj.days,
                                   cstart = subj.cstart,
                                   cend = subj.cend,
                                   units = subj.units,
                                   fname = prof.fname,
                                   lname = prof.lname,
                                   status = prof.status,
                                   department = dep.dname,
                                   isSelected = false
                               }).ToList();

        return View(asd);
}
publicActionResult类(字符串关键字=”)
{
EmployeeContext emp=新EmployeeContext();
列出asd=(来自emp.Subject中的Subc
在emp中加入prof。professors on subj.id等于prof.id
在prof.id等于dep.id的emp.departments中加入dep
其中subg.subg==关键字
选择新类列表()
{
id=主体id,
subc=subc.subc,
天数=附属天数,
cstart=sub.cstart,
cend=主体cend,
单位=主体单位,
fname=教授fname,
lname=教授lname,
地位=教授地位,
部门=部门名称,
isSelected=false
}).ToList();
返回视图(asd);
}
我研究过它,它说使用“全部”,但它不起作用。我不想根据关键字是空还是否来生成if-else语句,因为这会让我的代码很难看。
sub
属性是主题的名称。

您可以这样做

where subj.subj == keyword || keyword==""
或者这个,这样您就不需要一个单独的
where

from subj in emp.Subjects.Where(x=>x.subj == keyword || keyword=="")
join prof in emp.professors.....
你可以这样做

where subj.subj == keyword || keyword==""
或者这个,这样您就不需要一个单独的
where

from subj in emp.Subjects.Where(x=>x.subj == keyword || keyword=="")
join prof in emp.professors.....

除了@DarkKnight提供的答案外,您还可以利用这样一个事实,即在使用
ToList
实现查询之前,查询不会实际执行,因此您可以这样做:

public ActionResult classes(string keyword ="")
{
    EmployeeContext emp = new EmployeeContext();

    IEnumerable<classlist> asd = (from subj in emp.Subjects
                           join prof in emp.professors on subj.id equals prof.id
                           join dep in emp.departments on prof.id equals dep.id
                           select new classlist()
                           {
                               id = subj.id,
                               subj = subj.subj,
                               days = subj.days,
                               cstart = subj.cstart,
                               cend = subj.cend,
                               units = subj.units,
                               fname = prof.fname,
                               lname = prof.lname,
                               status = prof.status,
                               department = dep.dname,
                               isSelected = false
                           });

    //Apply the where clause if required
    if(!string.IsNullOrEmpty(keyword))
        asd = asd.Where(c => c.subj == keyword);

    //Return the materialised list now:
    return View(asd.ToList());
}
publicActionResult类(字符串关键字=”)
{
EmployeeContext emp=新EmployeeContext();
IEnumerable asd=(来自emp.Subject中的Subject
在emp中加入prof。professors on subj.id等于prof.id
在prof.id等于dep.id的emp.departments中加入dep
选择新类列表()
{
id=主体id,
subc=subc.subc,
天数=附属天数,
cstart=sub.cstart,
cend=主体cend,
单位=主体单位,
fname=教授fname,
lname=教授lname,
地位=教授地位,
部门=部门名称,
isSelected=false
});
//如果需要,应用where条款
如果(!string.IsNullOrEmpty(关键字))
asd=asd.Where(c=>c.subc==关键字);
//立即返回具体化列表:
返回视图(asd.ToList());
}
你为什么要这么做

  • 您可能经常有一个更复杂的where子句要应用,并且这种方式更容易编码
  • 当复合where子句不是必需的时候,它不会被传递到您的数据存储区,因此这个方法可能会稍微高效一些

  • 除了@DarkKnight提供的答案外,您还可以利用这样一个事实,即在使用
    ToList
    实现查询之前,查询不会实际执行,因此您可以这样做:

    public ActionResult classes(string keyword ="")
    {
        EmployeeContext emp = new EmployeeContext();
    
        IEnumerable<classlist> asd = (from subj in emp.Subjects
                               join prof in emp.professors on subj.id equals prof.id
                               join dep in emp.departments on prof.id equals dep.id
                               select new classlist()
                               {
                                   id = subj.id,
                                   subj = subj.subj,
                                   days = subj.days,
                                   cstart = subj.cstart,
                                   cend = subj.cend,
                                   units = subj.units,
                                   fname = prof.fname,
                                   lname = prof.lname,
                                   status = prof.status,
                                   department = dep.dname,
                                   isSelected = false
                               });
    
        //Apply the where clause if required
        if(!string.IsNullOrEmpty(keyword))
            asd = asd.Where(c => c.subj == keyword);
    
        //Return the materialised list now:
        return View(asd.ToList());
    }
    
    publicActionResult类(字符串关键字=”)
    {
    EmployeeContext emp=新EmployeeContext();
    IEnumerable asd=(来自emp.Subject中的Subject
    在emp中加入prof。professors on subj.id等于prof.id
    在prof.id等于dep.id的emp.departments中加入dep
    选择新类列表()
    {
    id=主体id,
    subc=subc.subc,
    天数=附属天数,
    cstart=sub.cstart,
    cend=主体cend,
    单位=主体单位,
    fname=教授fname,
    lname=教授lname,
    地位=教授地位,
    部门=部门名称,
    isSelected=false
    });
    //如果需要,应用where条款
    如果(!string.IsNullOrEmpty(关键字))
    asd=asd.Where(c=>c.subc==关键字);
    //立即返回具体化列表:
    返回视图(asd.ToList());
    }
    
    你为什么要这么做

  • 您可能经常有一个更复杂的where子句要应用,并且这种方式更容易编码
  • 当复合where子句不是必需的时候,它不会被传递到您的数据存储区,因此这个方法可能会稍微高效一些

  • 你可以在where子句中添加OR,writed | | String.IsNullOrEmpty(关键字)你可以在where子句中添加OR,writed | | String.IsNullOrEmpty(关键字)哦,我明白了,谢谢你的信息。这是非常有用的哦,我明白了,谢谢你的信息。这很有帮助