问题描述
我搜索了几个小时,没有任何运气.我正在尝试创建一个lambda表达式,以从两个表Schedule和Request中获取数据.但是我在这里输出一个布尔值.我如何做一个合适的左外连接来解决这个问题?这是我能想到的最好的
I searched hours and hours for this without any luck. I'm trying to create a lambda expression to fetch data from two tables Schedule and Request. But i'm outputting a bool here. How can i do a proper left outer join to fix this?this is the best i could come up with
ViewBag.RequestList = db.Requests
.Include(r => r.Department)
.Select(r => db.Schedules.Any(s => s.RequestId == r.RequestId));
但不是列表.
假设我的表格模型如下
public class Request{
public virtual int RequestId { get; set; }
public virtual string Remarks { get; set; }
}
public class Schedule{
public virtual int ScheduleId{ get; set; }
public virtual string Name{ get; set; }
public virtual Request Request { get; set; }
}
我正在尝试查看每个请求是否都具有一个或多个与之关联的时间表.因此,如果我可以将日程表对象附加到请求并将其输出为列表,那么多数民众赞成在我所需要的.但是我想使用LINQ和lambda表达式来做到这一点,而且我看到了如下查询;
I'm trying to see if each and every request has one or more schedules associated with it or not. so if i could attach schedule object to request and output it as a list then thats all i need. But I want to do it using LINQ and lambda expressions and I've seen queries as below;
var leftList = (from emp in db.Requests
join d in db.Schedules
on emp.RequestId equals d.RequestId into output
from j in output.DefaultIfEmpty()
select new { RequestId = emp.RequestId,
name = emp.Department.Name,
route = emp.Route.Name });
但这不是我想要的,因为我必须在new { RequestId = emp.RequestId, name = emp.Department.Name, route = emp.Route.Name }
But that's not what i want, because i have to specify every field i need in new { RequestId = emp.RequestId, name = emp.Department.Name, route = emp.Route.Name }
非常感谢!
推荐答案
只列出您想要的内容:
var leftList = from emp in db.Requests
join d in db.Schedules
on emp.RequestId equals d.RequestId into output
from j in output.DefaultIfEmpty()
select new
{
RequestId = emp.RequestId,
name = emp.Department.Name,
route = emp.Route.Name,
ScheduleId=j==null?0:j.ScheduleId,
SName=j==null?""j.Name,
};
这篇关于如何在EF ASP.NET MVC5中的lambda表达式中创建简单的左外部联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!