Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq - Equivalent to BETWEEN inside a Left Join
    primarykey
    data
    text
    <p>I've seen this topic <a href="https://stackoverflow.com/questions/4054982/between-equivalent-in-linq">BETWEEN EQUIVALENT in LINQ</a></p> <p>My Original Query in SQL:</p> <pre><code>SELECT ISNULL(Tcar.name, '') FROM dbo.models model LEFT JOIN cars Tcar on Tcar.model = model.id AND Tcar.year between model.Start and model.End </code></pre> <p>I need to implement between inside a "left join", I tried this:</p> <p>My Classes:</p> <pre><code>public class car { public string name { get; set; } public int model { get; set; } public DateTime year { get; set; } } public class model { public int id { get; set; } public DateTime Start { get; set; } public DateTime End { get; set; } } </code></pre> <p>My Implementation:</p> <pre><code>var theModel = from model in models join Tcar in cars on new { ID = (int)model.id, DateStart = (DateTime)model.Start, DateEnd = (DateTime)model.End } equals new { ID = (int)Tcar.model, DateStart = (DateTime)Tcar.year, DateEnd = (DateTime)Tcar.year } into tempCar from finalCar in tempCar select new { CAR = (finalCar == null ? String.Empty : finalCar.name) }; </code></pre> <p>WorkAround:</p> <pre><code>var theModel = from model in models join Tcar in cars on model.id equals Tcar.model where model.Start &lt;= Tcar.year &amp;&amp; model.End &gt;= Tcar.year select new { CAR = Tcar.name }; </code></pre> <p>If I use a workaround Linq translate to this query:</p> <pre><code>SELECT Tcar.name FROM dbo.models model LEFT JOIN cars Tcar on Tcar.model == model.id WHERE model.Start &lt;= Tcar.year and model.End &gt;= Tcar.year </code></pre> <p>I can put a simple where before "select new", but I have to implement by this way, with "between" inside the left join, How can I do this ?</p>
    singulars
    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.
 

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