2010-04-14 135 views
3

我有一个SQL查询,如下所示:的LINQ to SQL查询

Declare @DivisionNo INT 

    SET @DivisionNo = 5117 



    SELECT distinct CASE WHEN ISNULL([DivisionNo],'') <> @DivisionNo 
         THEN @DivisionNo ELSE [DivisionNo] END as DivisionNo 

     --,[RecordID]  
     ,[AcctCat]  
     ,[AcctCatDesc]  
     ,[CostCode]  
     ,[CostCodeDesc] 

    FROM [dbo].[vw_eSchdl_AcctCat_CostCode]  
    WHERE DivisionNo = @DivisionNo 

    UNION 

    SELECT distinct CASE WHEN ISNULL([DivisionNo],'') <> @DivisionNo 
         THEN @DivisionNo ELSE [DivisionNo] END as DivisionNo 

     --,[RecordID]  
     ,[AcctCat]  
     ,[AcctCatDesc]  
     ,[CostCode]  
     ,[CostCodeDesc] 

    FROM [dbo].[vw_eSchdl_AcctCat_CostCode]  
    WHERE AcctCat not in (
     SELECT [AcctCat]  
     FROM [dbo].[vw_eSchdl_AcctCat_CostCode] 
     WHERE DivisionNo = @DivisionNo 
) 

使用LINQ to SQL的它怎么能我重复?

感谢

+0

你尝试过什么?你可以得到一部分,但卡住某处?你可以发布你有什么?哪一位给你带来问题?或者你不知道从哪里开始? – 2010-04-14 18:24:15

回答

0

如果你有一点时间,没有更好的学习方法,比使用linqpad自己做。您可以使用SQL打开的选项卡和使用LINQ的选项卡,并尝试复制您的查询结果。

2

您可以从SQL转换使用的LINQ到Linqer。您可以从here下载。

+0

@Mark - LinqPad不会将SQL转换为Linq。不过,一款名叫Linqer的产品会。 – 2010-04-14 18:27:30

+0

@Randy:哎呀!修正了,谢谢。 – 2010-04-14 18:28:46

0

功能等同性如何?

int divisionNo = 5117; 

var matches = from ac in context.AcctCatCostCodes 
       where ac.DivisionNo == divisionNo 
       select ac; 

var missingAcctCat = from ac in matches 
        select ac.AcctCat; 

var others = from ac in context.AcctCatCostCodes 
      where !missingAcctCat.Contains(ac.AcctCat) 
      select ac; 

var union = from ac in matches.Union(others) 
      select new 
      { 
       DivisionNo = ac.DivisionNo ?? divisionNo, 
       ac.AcctCat, 
       ac.AcctCatDesc, 
       ac.CostCode, 
       ac.CostCodeDesc 
      }; 

...相同的方法,而不是查询语法...

var matches = context.AcctCatCostCodes 
        .Where(ac => ac.DivisionNo == divisionNo); 

var missingAcctCat = matches.Select(ac => ac.AcctCat); 

var others = context.AcctCatCostCodes 
        .Where(ac => !missingAcctCat.Contains(ac.AcctCat)); 

var union = matches.Union(others).Select(ac => 
      new 
      { 
       DivisionNo = ac.DivisionNo ?? divisionNo, 
       ac.AcctCat, 
       ac.AcctCatDesc, 
       ac.CostCode, 
       ac.CostCodeDesc 
      }); 

...通过LINQ2SQL生成的SQL ...

SELECT COALESCE([t4].[DivisionNo],@p2) AS [DivisionNo], 
     [t4].[AcctCat], 
     [t4].[AcctCatDesc], 
     [t4].[CostCode], 
     [t4].[CostCodeDesc] 
FROM (
    SELECT [t3].[AcctCat], [t3].[AcctCatDesc], [t3].[CostCode], 
      [t3].[CostCodeDesc], [t3].[DivisionNo] 
    FROM (
     SELECT [t0].[RecordID], [t0].[AcctCat], [t0].[AcctCatDesc], 
       [t0].[CostCode], [t0].[CostCodeDesc], [t0].[DivisionNo] 
     FROM [AcctCatCostCode] AS [t0] 
     WHERE [t0].[DivisionNo] = @p0 
     UNION 
     SELECT [t1].[RecordID], [t1].[AcctCat], [t1].[AcctCatDesc], 
       [t1].[CostCode], [t1].[CostCodeDesc], [t1].[DivisionNo] 
     FROM [AcctCatCostCode] AS [t1] 
     WHERE NOT (EXISTS(
      SELECT NULL AS [EMPTY] 
      FROM [AcctCatCostCode] AS [t2] 
      WHERE ([t2].[AcctCat] = [t1].[AcctCat]) 
        AND ([t2].[DivisionNo] = (@p1)) 
      )) 
     ) AS [t3] 
    ) AS [t4]