Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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#_.net_Sql_Linq_Stored Procedures - Fatal编程技术网

C# SQL到Linq等价物

C# SQL到Linq等价物,c#,.net,sql,linq,stored-procedures,C#,.net,Sql,Linq,Stored Procedures,我有一个存储过程,我想将它转换为LINQ到SQL,但我遇到了一些麻烦,因为我是LINQ新手,实际上,我不是SQL专家,也没有得到预期的结果。我尝试将存储过程从LINQ调用到SQL,但是当我在存储过程中以参数的形式发送期间datetime时,我在L2S上得到了一些关于参数可为null的错误,但是我也发送了不可为null的存储过程,我仍然得到了相同的错误。以下是SQL: SELECT Persons.IDPerson,Persons.Name,Persons.PaternalName,Depart

我有一个存储过程,我想将它转换为LINQ到SQL,但我遇到了一些麻烦,因为我是LINQ新手,实际上,我不是SQL专家,也没有得到预期的结果。我尝试将存储过程从LINQ调用到SQL,但是当我在存储过程中以参数的形式发送期间datetime时,我在L2S上得到了一些关于参数可为null的错误,但是我也发送了不可为null的存储过程,我仍然得到了相同的错误。以下是SQL:

SELECT  Persons.IDPerson,Persons.Name,Persons.PaternalName,Departments.DepartmentName,Jobs.Title, Persons.HireDate, Terminations.TerminationDate, Terminations.HireDate
FROM Persons left outer join Terminations on Persons.IDPerson = Terminations.IDPerson
             left outer join Departments on Departments.idDepartment = Persons.IdDepartment
             left outer join Jobs on Jobs.IDJob = Persons.IDJob          
WHERE (Terminations.IDTermination is null OR    Terminations.TerminationDate >= @Period) 
       and Terminations.HireDate <= @Period OR Persons.HireDate <=@Period
ORDER BY Persons.HireDate, Terminations.HireDate asc
这是我的LINQ代码,到目前为止,它确实可以编译,但它没有给我预期条件的记录。句点是一个可为空的日期时间:

 result = from emp in HRSystemDB.Persons.OfType<Employee>()
                     join term in HRSystemDB.Terminations on emp.IDPerson equals term.IDPerson into all
                     from aHire in all.Where(t => (t.IDTermination == null || t.TerminationDate.Date >= Criteria.Period.Value.Date)
                         && t.HireDate.Value.Date <= Criteria.Period.Value.Date
                         || emp.HireDate.Date <= Criteria.Period.Value.Date).DefaultIfEmpty()
                     select new DepartmentHeadCountQuery
                     {
                         FullName = emp.Name + " " + emp.PaternalName,
                         Department = emp.Department.DepartmentName,
                         JobTitle = emp.Job.Title,
                         TermDate = aHire.TerminationDate,
                         EHiredDate = emp.HireDate,
                         TermHireDate = aHire.TerminationDate
                     };

提前感谢。

此免费试用版可用于:

Linqer是一个SQL到LINQ转换工具。 它将帮助您学习LINQ并转换现有的SQL语句。 并非每个SQL语句都可以转换为LINQ,但Linqer涵盖了许多不同类型的SQL表达式。 Linqer支持.NET语言C和Visual Basic。 和一个完全免费的工具:

LINQPad也是学习LINQ的好方法:它预装了书中的200个例子,简而言之就是C3.0。没有比这更好的方式来体验LINQ和函数式编程的凉爽。
我知道这个问题很老,但我认为它需要一个正确的答案,我开始搜索与IN-SQL关键字等效的LINQ,发现没有提供此查询的等效LINQ代码,可能原作者不久前回答了这个问题,但我将此留给在internet上搜索的其他人:

如果你想留下外部连接,我要回答一个问题:

var query = from emp in context.Persons
            join ter in context.Terminations on emp.Id equals ter.IdPerson into terminations
            join dep in context.Departments on  emp.IdDeparment equals dep.IdDepartment into departments
            join job in context.Jobs on  emp.IdJob equals job.IdJob into jobs
            from ter in terminations.DefaultIfEmpty() //Does an Left Outer Join
            from dep in departments.DefaultIfEmpty()
            from job in jobs.DefaultIfEmpty()
            where (ter.IdTermination == null || ter.TerminationDate >= period)
            && (ter.HireDate <= period || emp.HireDate <=period)
            select new 
            { 
                emp.Id, 
                emp.Name,
                emp.PaternalName,
                dep.DepartmentName, 
                job.Title,  
                emp.HireDate,
                ter.TerminationDate,
                TerminationHireDate=ter.HireDate
                };  
一个使用内部连接

    var query = from emp in context.Persons
            join ter in context.Terminations on emp.Id equals ter.IdPerson 
            join dep in context.Departments on  emp.IdDeparment equals dep.IdDepartment 
            join job in context.Jobs on  emp.IdJob equals job.IdJob 
            where (ter.IdTermination == null || ter.TerminationDate >= period)
            && (ter.HireDate <= period || emp.HireDate <=period)
            select new 
            { 
                emp.Id, 
                emp.Name,
                emp.PaternalName,
                dep.DepartmentName, 
                job.Title,  
                emp.HireDate,
                ter.TerminationDate,
                TerminationHireDate=ter.HireDate
                };      

变量上下文是Linq到SQL或Linq到实体上下文

谢谢您的回答。我知道LINQPad,但我想我会试试。