【问题标题】:How to use Inner Join with Lambda expression?如何将内连接与 Lambda 表达式一起使用?
【发布时间】:2019-12-03 00:13:15
【问题描述】:

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

这是我在 SQL 中的查询:

SELECT * FROM 门票 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
);

【问题讨论】:

  • where 子句有什么问题?
  • 你试过了,太棒了!但结果如何?
  • 也许我的SQL to LINQ Recipe 可以帮助你。
  • 为什么你的LINQ有x.statusID != Closed而你的SQL有t.StatusId = 3

标签: c# sql linq lambda


【解决方案1】:

这些对我很有效:

  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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多