将SQL转换为LINQ

时间:2014-04-24 16:43:46

标签: sql vb.net linq

我一直试图将我的SQL转换为LINQ,但我似乎无法做到正确。我的计数没有给出正确的结果。

这是我的SQL:

   Select 
       max(batches.matcode) as MATCODE,
       max(batches.batchno) as batchno,
       folders.folderno,
       max(orders.sampdate),
       max(batches.batchID), 
       count(case servgrp when 'chimie' then 1 else null end),
       count(case servgrp when 'Chimie - LC'  then 1 when 'Chimie - AA' then 1 when  'Chimie - GC' then 1 else null end) ,
       count(case servgrp when 'Microbiologie' then 1 else null end),
       count(case when servgrp not in ('chimie','Chimie - LC' , 'Chimie - AA','Chimie - GC','Microbiologie') then 1 else null end),

   from batches 
   join folders on batches.batchno= folders.batchno and batches.batchid = folders.batchid
   join orders on orders.folderno = folders.folderno
   join ordtask on orders.ordno= ordtask.ordno
   join MaterialSYNONIMS on MATERIALSYNONIMS.MATCODE = batches.matcode

   where (batches.BATCHSTATUS <> 'Released' And batches.BATCHSTATUS <> 'Canceled' And batches.BATCHSTATUS <> 'Rejected') 
         and MATERIALSYNONIMS.MATSYNONIM='bulk'

   group by folders.folderno 

这是我目前在LINQ中所拥有的内容

Dim combinedTables = From batches In _lims_db.BATCHES
                     Join folders In _lims_db.FOLDERS On batches.BATCHNO Equals folders.BATCHNO And batches.BATCHID Equals folders.BATCHID
                     Join orders In _lims_db.ORDERS On folders.FOLDERNO Equals orders.FOLDERNO
                     Join ordetask In _lims_db.ORDTASKs On orders.ORDNO Equals ordetask.ORDNO
                     Join matsyn In _lims_db.MATERIALSYNONIMS On matsyn.MATCODE Equals batches.MATCODE
                     Where matsyn.MATSYNONIM = vMatType _
                           And (batches.BATCHSTATUS <> "Released" And batches.BATCHSTATUS <> "Canceled" And batches.BATCHSTATUS <> "Rejected")

                     Group folders, orders By folders.FOLDERNO Into combined = Group
                     Select MATCODE = combined.FirstOrDefault.folders.MATCODE, MATNAME = combined.FirstOrDefault.folders.BATCH.MATERIAL.MATNAME, BATCHNO = combined.FirstOrDefault.folders.BATCHNO, ORDNO = combined.FirstOrDefault.folders.FOLDERNO, SAMPDATE = combined.FirstOrDefault.orders.SAMPDATE, BATCHID = combined.FirstOrDefault.folders.BATCHID, ORIGREC = combined.FirstOrDefault.folders.ORIGREC, _
                     physicalTestComplete = combined.FirstOrDefault.orders.ORDTASKs.Where(Function(c) c.SERVGRP = "chimie" AndAlso c.RESULTS.All(Function(r) r.S = "done")).Count, physicalTotal = combined.FirstOrDefault.orders.ORDTASKs.Where(Function(c) c.SERVGRP = "chimie").Count, _
                     instrumentationTestComplete = combined.FirstOrDefault.orders.ORDTASKs.LongCount(Function(c) c.SERVGRP = "Chimie - LC" And c.SERVGRP = "Chimie - AA" And c.SERVGRP = "Chimie - GC" AndAlso c.RESULTS.All(Function(r) r.S = "done")), instrumentationTotal = combined.FirstOrDefault.orders.ORDTASKs.LongCount(Function(c) c.SERVGRP = "Chimie - LC" Or c.SERVGRP = "Chimie - AA" Or c.SERVGRP = "Chimie - GC"), _
                     microbiologyTestComplete = combined.FirstOrDefault.orders.ORDTASKs.Count, microbiologyTotal = combined.FirstOrDefault.orders.ORDTASKs.LongCount(Function(c) c.SERVGRP = "Microbiologie"), _
                     otherTestComplete = combined.FirstOrDefault.orders.ORDTASKs.Count, otherTotal = combined.FirstOrDefault.orders.ORDTASKs.LongCount(Function(c) c.SERVGRP <> "chimie" And c.SERVGRP <> "Chimie - LC" And c.SERVGRP <> "Chimie - AA" And c.SERVGRP <> "Chimie - GC" And c.SERVGRP <> "Microbiologie"), _
                     ReviewedComplete = combined.FirstOrDefault.orders.ORDTASKs.Count, ReviewedTotal = combined.FirstOrDefault.orders.ORDTASKs.Count

任何人都可以帮我把SQL转换成LINQ 谢谢

0 个答案:

没有答案