I have to make some queries in C# using LINQ. I tried my logic with a SQL request. It looks like this:
SELECT DISTINCT test_laktationen.lom, test_laktationen.laktanfang, test_laktationen.laktende, b_milch_hms_diagnose.diagnose
FROM test_laktationen INNER JOIN
b_milch_hms_diagnose ON test_laktationen.lom = b_milch_hms_diagnose.lom AND b_milch_hms_diagnose.behanddatum BETWEEN
test_laktationen.laktanfang AND test_laktationen.laktende INNER JOIN
b_milch_hms_kalbung ON test_laktationen.lom = b_milch_hms_kalbung.lom AND test_laktationen.laktanfang = b_milch_hms_kalbung.kalbedatum
WHERE (b_milch_hms_diagnose.diagnose LIKE '6.01.01.%' OR
b_milch_hms_diagnose.diagnose LIKE '6.01.02.%' OR
b_milch_hms_diagnose.diagnose LIKE '6.01.03.%' OR
b_milch_hms_diagnose.diagnose LIKE '6.01.04.%') AND (b_milch_hms_kalbung.gebverlauf = 3 OR
b_milch_hms_kalbung.gebverlauf = 4)
This works like it should, but now I have to make this request from data tables. Is this possible with LINQ? What would be good practice to solve this problem?
You would need a data context to perform direct access. I would recommend Linq2Sql classes file (*.dbml) to get it started. Once you create your data structure in the Linq2Sql class with the server explorer, your linq statement would look something like:
This will probably require tweaking in the final “where” clause to make sure you get the “between” portion correct.
BETWEENdoesn’t translate natively into from Linq, but it can be coded in as long as you can predict which value is likely to be greater.