c# - 在 linq 上使用 where all

标签 c# asp.net-mvc linq

我正在尝试为我的应用程序实现一个搜索功能,但我想要发生的是在没有指定关键字并且我有一个 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/

相关文章:

c# - 动态Where Linq 到实体

c# - 从 HTML 表中获取数据到数据表中

.net - Entity Framework : is there a complete list of supported IQueryable extension methods?

c# - 引用所需的重载泛型方法

c# - 如何存储和序列化列表以避免长字符串

c# - 形状与 DrawingVisual 的性能

c# - ASP.NET C# 将对象传递给方法

c# - MiniProfiler、EntityFramework 代码优先和后台任务 nullreference

asp.net-mvc - ModelState.AddModelError 不显示任何消息

asp.net - 在处理潜在的覆盖后,如何遵循 ASP.NET MVC 的 "default"HttpHandler?