如何在Lambda表达式中使用内部联接?

时间:2019-07-24 19:09:01

标签: c# sql linq lambda

我在SQL中有一个查询,需要转换为lambda表达式,但我不确定如何使用WHERE函数。

这是我在SQL中的查询:

选择*从票证t t.GroupId = g.GroupId上的INNER JOIN组g 其中g.UserId = 4 AND t.StatusId = 3或t.UserCreatedBy = 4 AND t.StatusId = 3

我尝试通过以下方式进行转换:

var query = db.Tickets
.Include(t => t.CftGroup)
.Include(t => t.Customer)
.Include(t => t.Factory)
.Include(t => t.Failure)
.Include(t => t.Line)
.Include(t => t.Priority)
.Include(t => t.Process.ProcessGroup)
.Include(t => t.Site)
.Include(t => t.Status)
.Join(db.Groups, t => t.CftGroupId, g => g.CftGroupId, (t, g) => new
{
    userCreatedBy = t.UserCreatedBy,
    userId = g.UserId,
    statusId = t.StatusId
})
.Where(
x => x.userId == id
&& x.statusId != Closed
|| x.userCreatedBy = id
&& x.statusId != Closed
);

1 个答案:

答案 0 :(得分:0)

这些对我来说很好:

  var TicketList =   Tickets
    .Join( Groups,
         ticket => ticket.GroupID,
         group => group.ID,
         (customer , group ) => new { Ticket = ticket, Group = group})
.Where(x => x.Group.UserId == 4 && (x.Ticket.StatusId == 3 || x.Ticket.UserCreatedBy == 4) && x.Ticket.StatusId == 3)
         .Select(select => select.Ticket ).ToList();