将SQL转换为LINQ:范围变量与先前的声明冲突

时间:2012-08-09 13:24:37

标签: c# sql linq entity-framework sql-to-linq-conversion

我正在尝试将这个相当复杂的(从我的角度来看,因为我不处理SQL)查询转换为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

这就是我到目前为止所做的:

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
                 {

                 };

但是这抱怨说“范围变量'h'与先前'h'的声明冲突。我明白它说它就像是第二个声明,但我不知道我怎么会在LINQ中这样做任何对此(完整或部分:)的帮助都将非常感谢!

//编辑:

如果我改变了 如建议的那样from h in hf.DefaultIfEmpty()from h1 in hf.DefaultIfEmpty(),h1与h具有相同的属性。所以我不能做第二次加入,因为桌子不在那里......

3 个答案:

答案 0 :(得分:2)

您需要更改

from h in hf.DefaultIfEmpty() 

到另一个变量

例如:

from h1 in hf.DefaultIfEmpty() 

不确定是否需要这些DefaultIfEmpty()行:这有用吗?

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 
    join s in context.stepdefinition_master on new { h.stepid, h.LocId } equals new { s.stepid, s.LocId } into hs 
    where h.file_no == fileNumber 
    orderby h.endtime 
    select new { };     

答案 1 :(得分:1)

您的查询中有from h in次。重命名其中一个h变量。

答案 2 :(得分:0)

我明白了。像其他答案一样,我有一个双重声明,但加入也不正确。正确的方法是这样的:(作为Lambda表达式)

 IList<HistoryView> resultList2 = context.History_master.Join(context.flowdefinition_master, h=> new {h.flowid, h.LocId}, f=>new{f.flowid, f.LocId}, (h,f) => new {h,f})
                                                            .Join(context.stepdefinition_master, h=> new {h.h.flowid, h.h.LocId}, s=>new{s.flowid, s.LocId}, (h,s) => new {h,s})
                                                            .Where(x=>x.h.h.file_no == fileNumber)
                                                            .Where(x => (x.h.h.status == "R") || (x.h.h.status == "U") || (x.h.h.status == "C") || (x.h.h.status == "K"))
                                                            .Select(r=> new HistoryView { 
                                                                flowDescription = r.h.f.description,
                                                                stepDescription = r.s.description, 
                                                                fileNumber = r.h.h.file_no,
                                                                taskId = r.h.h.taskid,
                                                                fromUser = r.h.h.from_userid,
                                                                userId = r.h.h.userid,
                                                                startTime = r.h.h.starttime,
                                                                lockTime = r.h.h.locktime,
                                                                lockBy = r.h.h.lockby,
                                                                status = r.h.h.status,
                                                                endTime = r.h.h.endtime
                                                            }).ToList();

感谢所有建议:)