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' の以前の宣言と競合します。2 番目の宣言のようなものだと言っていることは理解していますが、LINQ でこれをどのように行うかはわかりません。何か助けてください。これで(完全または部分的:))大歓迎です!

//編集:

提案どおりに変更する from h in hf.DefaultIfEmpty()from h1 in hf.DefaultIfEmpty()、h1 には h と同じプロパティがありません。テーブルがないため、2番目の結合を実行できません...

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クエリに 2 回あります。h変数の 1 つの名前を変更します。

于 2012-08-09T13:28:34.777 に答える
0

私はそれを考え出した。他の回答が言ったように、二重宣言がありましたが、結合も正しくありませんでした。正しい方法は次のとおりです: (ラムダ式として)

 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 に答える