2010-04-14 7 views
3

J'ai une requête SQL comme suit:LINQ to SQL requête

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 
) 

Comment puis-je dupliquer en utilisant LINQ to SQL?

Merci

+0

Qu'avez-vous essayé? Pouvez-vous obtenir en partie, mais rester coincé quelque part? Pouvez-vous poster ce que vous avez? Quel bit vous pose des problèmes? Ou n'avez-vous aucune idée par où commencer? –

Répondre

0

Il n'y a pas de meilleure façon d'apprendre que de le faire vous-même en utilisant linqpad si vous avez un peu de temps. Vous pouvez avoir un onglet avec SQL ouvert et un onglet avec LINQ et essayez de dupliquer les résultats de votre requête.

2

Vous pouvez convertir de SQL à l'aide de Linq Linqer. Vous pouvez le télécharger à partir de here.

+0

@Mark - LinqPad ne convertira pas SQL en Linq. Cependant, un produit nommé Linqer sera. –

+0

@Randy: Oups! Fixe, merci. –

0

Qu'en est-il fonctionnellement équivalent?

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 
      }; 

... même que des méthodes au lieu de la syntaxe de requête ...

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 
      }); 

... SQL généré par LINQ2SQL ...

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]