简体   繁体   English

SQL查询到LINQ(对我来说很复杂)

[英]SQL query to LINQ (complex for me)

I am a very new to LINQ, I have done simple queries, but I have hit a wall on this one. 我是LINQ的新手,我已经完成了一些简单的查询,但是我碰到了这堵墙。 Could someone help me convert this to LINQ? 有人可以帮我将其转换为LINQ吗? Joining the table to itself and the counting of records has me hung. 将表自身连接起来并计数记录使我感到吊死。

SELECT DISTINCT
     RegionID = O.Region,
     Region = R.Office,
     OfficeCount = (SELECT
          COUNT(officeID)
          FROM OFfice
          WHERE Region = O.region
          AND Type IN (5, 6)
          AND ClosedProduction = 0
          AND OfficeID NOT IN (10, 135, 151)
          AND (OfficeID IN (SELECT DISTINCT
                    OfficeID
                    FROM WR_Data_Work
                    WHERE PhaseID IS NOT NULL)
          OR OfficeID = 154))
     FROM office O
     JOIN Office R
     ON O.Region = R.OfficeID
     JOIN Employee VP
     ON R.VicePresID = VP.EmployeeID
     WHERE O.OfficeID NOT IN (10, 135, 151)
     AND O.Type IN (5, 6)
     AND O.ClosedProduction = 0
     AND (O.OfficeID IN (SELECT DISTINCT
                         OfficeID
                         FROM WR_Data_Work
                         WHERE PhaseID IS NOT NULL)
     OR O.OfficeID = 154)
ORDER BY RegionID
OfficeID    OfficeName  Region
88          Office 1    90
90          Office 2    90
96          Office 3    90
86          Office 4    93
91          Office 5    93
92          Office 6    93
93          Office 7    93
95          Office 8    93

In this case, Office 2 is a regional office for offices 1 and 3, but office 7 is a regional office for 4, 5, 6 and 8. 在这种情况下,办公室2是办公室1和3的区域办事处,而办公室7是办公室4、5、6和8的区域办事处。

Here is my try without any ability to test: 这是我没有任何测试能力的尝试:

var PhaseOffices = (from w in WR_Data_Work where w.PhaseID != null select w.OfficeID).Distinct();
var TargetOffices = from O in Office
                    where (O.Type == 5 || O.Type == 6) && O.ClosedProduction == 0 &&
                    (!(new[] { 10, 135, 151 }).Contains(O.OfficeID)) && (PhaseOffices.Contains(O.OfficeID) || O.OfficeID == 154)
                    select O;

var res = (from O in TargetOffices
          join R in Office on O.Region equals R.OfficeID
          //join VP in Employee on R.VicePresID equals VP.EmployeeID = not needed?
          orderby O.Region
          select new {
              RegionID = O.Region,
              Region = R.Office,
              OfficeCount = (from ofc in TargetOffices
                             where ofc.Region == O.Region
                             select ofc.OfficeID).Count()
           }).Distinct();

Some LINQ Providers won't handle the local array Contains I used, in which case you'll have to replace it with individual tests like I did with Type . 某些LINQ提供程序将无法处理我使用的本地数组Contains ,在这种情况下,您必须像使用Type一样用单独的测试替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM