Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL to Linq equivalent
    primarykey
    data
    text
    <p>I've got this stored procedure I'd like to convert to LINQ to SQL, but I'm having a bit of trouble because I'm new to LINQ (and actually, I'm no SQL guru) and I am not getting the expected results. (I tried calling the SPROC from LINQ to SQL but when I send in the Period datetime as parameter on the SPROC I get some error on L2S about the parameter being nullable but I sent it also non-nullable and I still get the same error.) Below is the SQL:</p> <pre><code>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 &gt;= @Period) and Terminations.HireDate &lt;= @Period OR Persons.HireDate &lt;=@Period ORDER BY Persons.HireDate, Terminations.HireDate asc </code></pre> <p>This is my LINQ code so far (it does compile but it doesn't give me the records I expect) The Criteria.Period is a nullable datetime:</p> <pre><code> result = from emp in HRSystemDB.Persons.OfType&lt;Employee&gt;() join term in HRSystemDB.Terminations on emp.IDPerson equals term.IDPerson into all from aHire in all.Where(t =&gt; (t.IDTermination == null || t.TerminationDate.Date &gt;= Criteria.Period.Value.Date) &amp;&amp; t.HireDate.Value.Date &lt;= Criteria.Period.Value.Date || emp.HireDate.Date &lt;= 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 }; </code></pre> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload