Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/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查询重写为LINQ查询?_C#_Sql Server_Linq - Fatal编程技术网

C# 如何将以下SQL查询重写为LINQ查询?

C# 如何将以下SQL查询重写为LINQ查询?,c#,sql-server,linq,C#,Sql Server,Linq,我想在LINQ中重写以下SQL查询。但我的问题是,我不知道如何使用LINQ左连接编写AND(&&)运算符(请看我的第二个左连接) 我试了下一个。但是得到编译错误- from emp in dbContext.EmployeeList join dsg in dbContext.hrmDesig on emp.HrmDesignationId equals dsg.Id into DSGLeftJoin

我想在LINQ中重写以下SQL查询。但我的问题是,我不知道如何使用LINQ左连接编写AND(&&)运算符(请看我的第二个左连接)

我试了下一个。但是得到编译错误-

from emp in dbContext.EmployeeList
                        join dsg in dbContext.hrmDesig on emp.HrmDesignationId equals dsg.Id into DSGLeftJoin
                        from dsglj in DSGLeftJoin.DefaultIfEmpty()
                        join pob in dbContext.PfOpeningBalances on emp.Id equals pob.HrmEmployeeId into POBLeftJoin
                        from poblj in POBLeftJoin.DefaultIfEmpty() && poblj.CmnCalendarYearId == clndrId
                        where emp.Id==empId
                        select new
                        {
                            empIdr = emp.Id,
                            EmployeeId = emp.EmployeeId,
                            EmployeeName = emp.Name,
                            Designation = dsglj.Name,
                            OpeningIncome = poblj.OpeningIncome,
                            EmployeeContribution = poblj.EmployeeContribution,
                            CompanyContribution = poblj.CompanyContribution
                        }
试试这个

 (from emp in HrmEmployees
  join dsg in HrmDesignations 
  on  emp.HrmDesignationId equals dsg.Id
  join pob in PfmOpeningBalance 
  on emp.Id equals pob.HrmEmployeeId AND pob.CmnCalendarYearId equals 2
  into eGroup
  from emps in eGroup.DefaultIfEmpty()
  emp.Id=6
  select new
    {
        EmployeeId =emp.EmployeeId,
        Name=dsg.Name,
        CompanyContribution=pob.CompanyContribution,
        EmployeeContribution=pob.EmployeeContribution,
        OpeningIncome=pob.OpeningIncome
    }).ToList();

好的,你可以这样试试

from emp in dbContext.EmployeeList
join dsg in dbContext.hrmDesig on emp.HrmDesignationId=dsg.Id
join pob in dbContext.PfOpeningBalanceson new {emp.Id, jp=pob.CmnCalendarYearId} equals new {pob.HrmEmployeeId, jp=2}
where emp.Id=6
select new {emp.EmployeeId,
            dsg.Name,
            pob.CompanyContribution,
            pob.EmployeeContribution,
            pob.OpeningIncome};
from emp in dbContext.EmployeeList.Where(e=>e.Id.Equals(6))
join dsg in dbContext.hrmDesig on emp.HrmDesignationId=dsg.Id
join pob in dbContext.PfOpeningBalanceson.Where(x=>x.CmnCalendarYearId.Equals(2)) on emp.Id equals pob.HrmEmployeeId
select new {emp.EmployeeId,
            dsg.Name,
            pob.CompanyContribution,
            pob.EmployeeContribution,
            pob.OpeningIncome};
或者像这样,

from emp in dbContext.EmployeeList
join dsg in dbContext.hrmDesig on emp.HrmDesignationId=dsg.Id
join pob in dbContext.PfOpeningBalanceson new {emp.Id, jp=pob.CmnCalendarYearId} equals new {pob.HrmEmployeeId, jp=2}
where emp.Id=6
select new {emp.EmployeeId,
            dsg.Name,
            pob.CompanyContribution,
            pob.EmployeeContribution,
            pob.OpeningIncome};
from emp in dbContext.EmployeeList.Where(e=>e.Id.Equals(6))
join dsg in dbContext.hrmDesig on emp.HrmDesignationId=dsg.Id
join pob in dbContext.PfOpeningBalanceson.Where(x=>x.CmnCalendarYearId.Equals(2)) on emp.Id equals pob.HrmEmployeeId
select new {emp.EmployeeId,
            dsg.Name,
            pob.CompanyContribution,
            pob.EmployeeContribution,
            pob.OpeningIncome};

你试过了吗?是的,但是编译错误。可能是wron syntax@irshad的错误,请在帖子中显示代码,以便我们提供帮助,否则我们不知道您使用什么来放置此linq。实体框架或实体类等。错误消息通常包含有用的信息。不要只告诉我们您“遇到错误”,请包含实际的错误消息。