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());
}
你为什么要这么做
除了@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子句中添加OR,writed | | String.IsNullOrEmpty(关键字)你可以在where子句中添加OR,writed | | String.IsNullOrEmpty(关键字)哦,我明白了,谢谢你的信息。这是非常有用的哦,我明白了,谢谢你的信息。这很有帮助