0

我正在尝试将这个相当复杂的(从我的角度来看,因为我不处理 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 相同的属性。所以我不能做第二次加入,因为桌子不在那里......

4

3 回答 3

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 { };     
于 2012-08-09T13:28:42.820 回答
1

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

于 2012-08-09T13:28:34.777 回答
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();

感谢所有的建议 :)

于 2012-08-09T17:48:03.507 回答