【问题标题】:Sql to LINQ query LINQ Query convertSql 到 LINQ 查询 LINQ 查询转换
【发布时间】:2016-08-27 17:42:35
【问题描述】:

我有一个 SQL 查询我想用 LINQ

编写

这是我的查询

SELECT DISTINCT * 
FROM [IHQDB].[dbo].[Table1] as t1 
inner join Table2 as t2 on t2.Table2 =t1.ChangedItemID 
inner join Table3 as t3 on t3.Table3 = t1.FromUserID 
where (t1.FromUserID=1 And t2.ContentItemID= t1.ChangedItemID) 
OR (t2.LastModifiedBy=1 or t2.CreatedBy=1 )

您好,现在它工作正常,但我的查询在 1 的位置有点不同,我需要我的 userID 基于 M_User 表中的名字和姓氏。 如何根据 First Name + Last Name 获取 UserId

这是我用于检索用户名的 LINQ 代码

linq4 = from q in context.T_ContentItems
join p in context.M_Users on q.CreatedBy equals p.UserID 
where (advanceKeyword.Contains(p.EmployeeFirstName + " " + p.EmployeeLastName)) select q; 
advancechk12 = linq4.ToList();

================================================ ==========================

我需要的是,无论我在哪里写了值“1”(例如t2.CreatedBy=1),我都需要找到 UserID。为简单起见,我能够在 advancechk12 中获取所有过滤用户的名称。如何检索 advancechk12

中返回的用户名列表的 UserID's

【问题讨论】:

  • 也发布您的实体模型。
  • 请在新帖子中提出您的第二个问题。这是一个新问题。届时您将获得更多反馈。

标签: entity-framework


【解决方案1】:

您必须将下面提到的 Linq 查询替换为您的模型名称。我只是使用了与 T-Sql 相同的名称。

 var t1List =   (from t1 in db.Table1
    join t2 in db.Table2 on t1.ChangedItemID equals t2.Id
    join t3 in db.Table3 on t3.Id equals t1.FromUserID
    where ((t1.FromUserID=1 && t2.ContentItemID= t1.ChangedItemID) || (t2.LastModifiedBy=1 or t2.CreatedBy=1))
    select t1).Distinct().ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-16
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    相关资源
    最近更新 更多