I saw this topic BETWEEN AN EQUIVALENT in LINQ
My original query in SQL:
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
I need to implement between the "left join", I tried this:
My classes:
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; } }
My implementation:
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) };
Decision:
var theModel = from model in models join Tcar in cars on model.id equals Tcar.model where model.Start <= Tcar.year && model.End >= Tcar.year select new { CAR = Tcar.name };
If I use the Linq workaround, translate this query:
SELECT Tcar.name FROM dbo.models model LEFT JOIN cars Tcar on Tcar.model == model.id WHERE model.Start <= Tcar.year and model.End >= Tcar.year
I can put a simple one where in front of โselect newโ, but I have to implement this way, โbetweenโ inside the left connection, how to do it?
source share