2016-08-16 28 views
0

我有2個SQL查詢我嘗試使用linqer轉換但沒有結果。查詢是這樣的:在LINQ中轉換SQL查詢並選擇計數

select distinct a.DOCID, 
(select dcrea from mstdocs where DOCID = a.docid and vtype = 'table') DOC_DATE, 
(select dcrea from mstdocs where DOCID= a.docid and vtype = 'read' and VTAID = '2') Read_DATE 
from mstdocs a, mstdocstats b 
where a.docid = b.docid 
and a.vtaid = '2' 
and a.vtype = 'read' 
and DATEPART(mm,a.DCREA) = '06' 
and DATEPART(yyyy,a.DCREA) = '2016' 
and a.docid in 
(
select distinct a.docid 
from mstdocs a, mstdocstats b 
where a.docid = b.docid 
and a.vtype = 'table' 
and DATEPART(mm,a.DCREA) = '06' 
and DATEPART(yyyy,a.DCREA) = '2016' 
) 


select distinct a.docid 
from mstdocs a, mstdocstats b 
where a.docid = b.docid 
and (select count(*) from mstdocs docs where docs.DOCID = a.docid and docs.vtaid = '2') = 1 

我有嘗試轉換第一個查詢分成兩個部分

  • 風險價值=(從b在mstdocstats 在mstdocs上a.docid加入等於B。的docID 其中a.vtype.Equals( 「表」) & & a.DCREA.Month == '06' & & a.DCREA.Year == '2016' 選擇a.docid).Distinct()。ToList ();

  • 變種B =(從步驟b中mstdocstats 加入在上a.docid mstdocs等於b.docid 其中a.Contains(a.docid) 選擇新的{ 文檔ID = a.docid, DOC_DATE = (從mstdocs其中a.docid == mstdoc.docid & & mstdoc.vtype == 「表」 mstdoc選擇
    mstdoc.DCREA),
    Read_DATE =(從mstdocs mstdoc其中a.docid == mstdoc.docid & & mstdoc.vtype ==「讀」& &
    mstdoc.vtaid ==「2」select mstdoc.DCREA)})。ToList();

但變得錯誤,有人可以告訴我哪裏是錯誤嗎?

感謝任何答案...

回答

0

我假設DCREADatetime ..第一個查詢最初得到的,在2016年6月日期間隔落在子集,並從該得到的「讀子讀「 ...

var qry_1 = (from a in (
       from a in mstdocs 
       join b in mstdocstats on a.docid equals b.docid 
       where a.DCREA.Month == 6 && a.DCREA.Year == 2016 
       select a 
      )join b in mstdocstats on a.docid equals b.docid 
      where a.vtaid == "2" && a.vtype == "read" 
      select new { 
         DOCID = a.Name, 
         Read_DATE = a.DCREA, 
         DOC_DATE = (from mstdoc in mstdocs where a.docid == mstdoc.docid && mstdoc.vtype == "table" select mstdoc.DCREA) 
        } 
      ).Distinct().ToList(); 




var qry_2 = (from a in 
      (from a in mstdocs 
       where a.vtaid == "2" 
       group a by a.docid into GroupMstdocs 
       where GroupMstdocs.Count() == 1 
       select new { docid = GroupMstdocs.Key } 
      ) join b in mstdocstats on a.docid equals b.docid 
       select a.docid 
      ).Distinct().ToList(); 
+0

是字段dcrea是日期時間,但我編譯該查詢和結果是標識符預期; 'in'是一個關鍵字,如果我想將該查詢存儲到列表中,我可以在distinct()後給tolist()嗎? –

+0

絕對是的..你可以添加toList()在查詢 –

+0

但我沒有結果,我不是爲什麼會變成錯誤:( –