我正在尝试为我的应用程序实现一个搜索功能,但我想要发生的是在没有指定关键字并且我有一个 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);
}
我对此进行了研究,它说要使用“ALL”,但它不起作用。我不想根据关键字是否为空来做 if else 语句,因为这会使我的代码难看。 subj
属性是主题的名称。
最佳答案
你可以这样做
where subj.subj == keyword || keyword==""
或者这个,这样你就不需要单独的where
from subj in emp.Subjects.Where(x=>x.subj == keyword || keyword=="")
join prof in emp.professors.....
关于c# - 在 linq 上使用 where all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917790/