Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将此SQL语句转换为Lambda/Linq表达式_C#_.net_Entity Framework_Linq_Linq To Entities - Fatal编程技术网

C# 如何将此SQL语句转换为Lambda/Linq表达式

C# 如何将此SQL语句转换为Lambda/Linq表达式,c#,.net,entity-framework,linq,linq-to-entities,C#,.net,Entity Framework,Linq,Linq To Entities,我正在为这个SQL语句寻找等效的LINQ Lambda表达式 SELECT A.USER_ID, A.GIVEN_NAME, A.SURNAME, A.REGION, A.EMAIL FROM USER A WHERE A.USER_ID IN ( SELECT B.USER_ID_TXT FROM TRAINING_COURSE B WHERE B.COURSE_ID_TXT IN (SELECT C.MITT_COURSE_ID_TXT

我正在为这个SQL语句寻找等效的LINQ Lambda表达式

SELECT A.USER_ID, A.GIVEN_NAME, A.SURNAME, A.REGION, A.EMAIL 
FROM USER A
WHERE A.USER_ID IN (
    SELECT B.USER_ID_TXT 
    FROM TRAINING_COURSE B
    WHERE B.COURSE_ID_TXT IN 
        (SELECT C.MITT_COURSE_ID_TXT 
            FROM TRAINING_MITT C 
            WHERE C.TRAINING_ID =
                (SELECT D.TRAINING_ID 
                 FROM TRAINING_ROLE D 
                 WHERE D.ROLE_ID = 3011)))
这里是实体。我添加了一些注释来说明对SQL查询中的表和字段的引用

此表存储用户信息,并作为物化视图装入

// TABLE USER 
public class TCUser
{
    // Field USER_ID
    public string UserId { get; set; }
    public string GivenName { get; set; }
    public string Surname { get; set; }
    public string OfficeBuilding { get; set; }
    public string Address { get; set; }
    public string FloorLocation { get; set; }
    public string FloorNumber { get; set; }
    public string Region { get; set; }
    public string Province { get; set; }
    public string PostalCode { get; set; }
    public string City { get; set; }
    public string Email { get; set; }
}  
下一个table store培训课程来自外部资源。它基本上是数据的导入

// TABLE TRAINING_COURSE - EXTERNAL SOURCE
public class TrainingCourse
{
    // Field USER_ID_TXT
    public String UserId { get; set; }

    // Field COURSE_ID_TXT
    public String CourseId { get; set; }
    public String CourseDescription { get; set; }
    public String ScheduleId { get; set; }
    public String ScheduleDescription { get; set; }
    public DateTime? ScheduleStartDate { get; set; }
    public DateTime? ScheduleEndDate { get; set; }
    public String Status { get; set; }
}
此表列出了履行角色所需的培训课程的关联

// TABLE TRAINING_ROLES - XREF between TRAINING_MITT and ICS_ROLE
public class IcsTrainingRole
{
     // Field ROLE_ID
     public int RoleId { get; set; }

     // Field TRAINING_ID
     public int TrainingId { get; set; }
     public virtual IcsTraining IcsTraining { get; set; }
     public virtual IcsRole IcsRole { get; set; }
}
此表显示了培训与外部手套培训数据库的关联

// TABLE TRAINING_MITT
public class IcsTrainingMitt
{
    // Field TRAINING_ID             
    public int TrainingId { get; set; }

    // Field MITT_COURSE_ID_TXT
    public string MittCourseId { get; set; }
    public virtual IcsTraining IcsTraining { get; set; }
}

public class IcsTraining
{
    public int TrainingId { get; set; }
    public string TrainingName { get; set; }
    public virtual ICollection<IcsTrainingRole> IcsTrainingRoles { get; set; }
    public virtual ICollection<IcsTrainingMitt> IcsTrainingMitt { get; set; }
}


public class IcsRole
{
    public int RoleId { get; set; }
    public int SectionId { get; set; }
    public string RoleName { set; get; }
    public virtual ICollection<IcsTrainingRole> IcsTrainingRoles { get; set; }
}
//表格训练\u MITT
公共类培训手套
{
//野外训练
public int TrainingId{get;set;}
//现场手套课程ID文本
公共字符串ID{get;set;}
公共虚拟IcsTraining IcsTraining{get;set;}
}
公开课培训
{
public int TrainingId{get;set;}
公共字符串TrainingName{get;set;}
公共虚拟ICCollection IcsTrainingRoles{get;set;}
公共虚拟ICCollection IcsTrainingMitt{get;set;}
}
公共类角色
{
public int RoleId{get;set;}
public int SectionId{get;set;}
公共字符串RoleName{set;get;}
公共虚拟ICCollection IcsTrainingRoles{get;set;}
}

如果您使用连接重新编写代码,它将如下所示:

SELECT A.USER_ID, A.GIVEN_NAME, A.SURNAME, A.REGION, A.EMAIL 
FROM USER A
JOIN TRAINING_COURSE B ON A.USER_ID = B.USER_ID_TXT
JOIN TRAINING_MITT C ON B.COURSE_ID_TXT = C.MITT_COURSE_ID
JOIN TRAINING_ROLE D ON C.TRAINING_ID = D.TRAINING_ID AND D.ROLE_ID = 3011

通过使用联接(正如Hogan所建议的那样),在模型类中使用Linq查询语法:


我认为查询语法应该比使用lambdas清楚得多,因为模型似乎缺少适当的导航属性。

好的,使用join实际上更简单,基于@kaffekopp的帖子,使用lambdas的等效表达式应该是:

var users = TCUser
    .Join(TrainingCourse, u => u.UserId, tc => tc.UserId, (u, tc) => new {u, tc})
    .Join(IcsTrainingMitt, @t => @t.tc.CourseId, tm => tm.MittCourseId, (@t, tm) => new {@t, tm})
    .Join(IcsTrainingRole, @t => @t.tm.TrainingId, tr => tr.TrainingId, (@t, tr) => new {@t, tr})
    .Where(@t => @t.tr.RoleId == 3011)
    .Select(@t => new
    {
        UserId = @t.@t.@t.u.UserId,
        GivenName = @t.@t.@t.u.GivenName,
        Surname = @t.@t.@t.u.Surname,
        Email = @t.@t.@t.u.Email
    });

不要。LINQ不是SQL,也不是SQL的替代品。这是一种与ORMs一起使用的语言。正确配置您的ORM实体,添加正确的导航关系和属性,并且您的ORM将从LINQ查询生成SQL语句如果执行此查询,我怀疑ORM最终也会得到更清晰的查询。没有理由在
后面使用子查询,其中,
中的.USER\u ID将生成相同的执行计划。请在问题中添加您的实体模型代码。@kaffekopp:使用实体模型更新问题。也许我的方法可以帮到您?
var users = TCUser
    .Join(TrainingCourse, u => u.UserId, tc => tc.UserId, (u, tc) => new {u, tc})
    .Join(IcsTrainingMitt, @t => @t.tc.CourseId, tm => tm.MittCourseId, (@t, tm) => new {@t, tm})
    .Join(IcsTrainingRole, @t => @t.tm.TrainingId, tr => tr.TrainingId, (@t, tr) => new {@t, tr})
    .Where(@t => @t.tr.RoleId == 3011)
    .Select(@t => new
    {
        UserId = @t.@t.@t.u.UserId,
        GivenName = @t.@t.@t.u.GivenName,
        Surname = @t.@t.@t.u.Surname,
        Email = @t.@t.@t.u.Email
    });