I’m trying to convert this rather complex (from my perspective as I don’t deal with SQL) query into LINQ:
SELECT f.description,
s.description,
file_no,taskid,
h.description
from_userid,
userid,
h.starttime,
locktime,
lockby,
h.status,
h.endtime
FROM history h
INNER JOIN flowdefinition f on h.flowid = f.flowid
INNER JOIN stepdefinition s on h.flowid = s.flowid and h.stepid = s.stepid
WHERE taskid = 'SERVER2012_03_08_09_31_40_367'
AND h.status in ('R','U','C','K')
AND h.flowid not in (999)
order by endtime
And this is what I have so far:
var resultList = from h in context.History_master
join f in context.flowdefinition_master on new { h.flowid, h.LocId } equals new { f.flowid, f.LocId } into hf
from h in hf.DefaultIfEmpty()
join s in context.stepdefinition_master on new { h.stepid, h.LocId } equals new { s.stepid, s.LocId } into hs
from s in hs.DefaultIfEmpty()
where h.file_no == fileNumber
orderby h.endtime
select new
{
};
But this complains that “The range variable ‘h’ conflicts with a previous declaration of ‘h’. I understand that it says it is like a second declaration, but I don’t know how else i would do this in LINQ. Any help with this (complete or partial 🙂 ) would be greatly appreciated!
//EDIT:
If i change the
from h in hf.DefaultIfEmpty() to from h1 in hf.DefaultIfEmpty() as suggested, the h1 does not have the same properties as h. So i can’t do the second join because the tables are not there…
I figured it out. Like other answers said, i had a double declaration, but also the join was not correct. The correct way is like this: (as a Lambda Expression)
Thanks for all the suggestions 🙂