2011-08-18 43 views
3

我需要为下面提到的SQL对应的LINQ查询,我与嵌套挣扎加入SQL中的LINQ嵌套的联接

SQL代码:

SELECT 
     * 
FROM 
     ((((table1 
     INNER JOIN 
     (table2 
     RIGHT JOIN 
     table3 
     ON table2.StID = table3.StID) 
     ON table1.SCode = table3.ECode) 
     LEFT JOIN 
     table4 
     ON table3.TypeID = table4.TypeID) 
     LEFT JOIN 
     table5 
     ON table3.ValueID = table5.ValueId) 
     LEFT JOIN 
     table2 AS table6 
     ON table3.[Num] = table6.StID) 
     LEFT JOIN 
     table5 AS table7 
     ON table3.[TValueID] = table7.ValueId 
WHERE 
     table2.Col1 = '1000' 

我试图打破在较小的查询件和尝试与最初的2个连接

I tried to make a Linq for 

    select * from 
    (table1 
    INNER JOIN 
    (table2 
    RIGHT JOIN 
    table3 
    ON table2.StID = table3.StID) 
    ON table1.SCode = table3.ECode) 


    Linq : 

    var query = from rightJoin in 
          (
           from t3 in table3 
           join t2 in table2 
           on t3.StID equals t2 .StID into joined 
           from T in joined.DefaultIfEmpty() 
           select new 
           { 
            A = t3, 
            B = T 
           } 
          ) 
         join T1 in table1 
         on rightJoin.A.ECode equals T1.SCode into innerjoin 
         select new 
         { 
          C = rightJoin.A.ECode 
         }; 

      int i = query.Select(a => a.C).ToList().Count; 

1)上述sql是否正确上述linq。我从sql和linq获得不同数量的记录,所以我相信linq代码是不正确的。

2)我需要将原始sql转换为linq。

任何帮助将不胜感激。

回答

1

也许你需要这样的东西。

示例类

public class table1 
{ 
    public string SCode { get; set; } 
} 

public class table2 
{ 
    public int StID { get; set; } 
    public string Col1 { get; set; } 
} 

public class table3 
{ 
    public int StID { get; set; } 
    public int TypeID { get; set; } 
    public int ValueID { get; set; } 
    public int Num { get; set; } 
    public int TValueID { get; set; } 
    public string ECode { get; set; } 
} 

public class table4 
{ 
    public int TypeID { get; set; } 
} 

public class table5 
{ 
    public int ValueId { get; set; } 
} 

Linq的执行情况:

var Select = from Table2 in dc.GetTable<table2>()        
         //Right Join              
         from Table3_3 in dc.GetTable<table3>() 
         .Where(item => item.StID == Table2.StID) 
         .Select(item => item)        
         //Inner Join From Right Join                      
         join Table1_3_1 in dc.GetTable<table1>()         
          on Table3_3.ECode equals Table1_3_1.SCode 
         //Left Join table4 
         join entityTable4 in dc.GetTable<table4>() 
          on Table3_3.TypeID equals entityTable4.TypeID into tempTable4 
         from Table4 in tempTable4.DefaultIfEmpty() 
         //Left Join table5 
         join entityTable5 in dc.GetTable<table5>() 
          on Table3_3.ValueID equals entityTable5.ValueId into tempTable5 
         from Table5 in tempTable5.DefaultIfEmpty() 
         //Left Join table2 (table6) 
         join entityTable2 in dc.GetTable<table2>() 
          on Table3_3.Num equals entityTable2.StID into tempTable2 
         from Table6 in tempTable2.DefaultIfEmpty() 
         //Left Join table5 (table7) 
         join entityTable5 in dc.GetTable<table5>() 
          on Table3_3.TValueID equals entityTable5.ValueId into tempTable5_7 
         from Table7 in tempTable5_7.DefaultIfEmpty() 
         //Filter 
         where Table2.Col1 == "1000" 
         select new 
         { 
          table1 = new { SCode = (string)Table1_3_1.SCode }, 
          table2 = new { StID = (int)Table2.StID, Col1 = (string)Table2.Col1 }, 
          table3 = new 
          { 
           StID = (int)Table3_3.StID, 
           TypeID = (int)Table3_3.TypeID, 
           ValueID = (int)Table3_3.ValueID, 
           TValueID = (int)Table3_3.TValueID, 
           Num = Table3_3.Num, 
           ECode = Table3_3.ECode 
          }, 
          table4 = Table4 == null ? null : new { TypeID = (int)Table4.TypeID }, 
          table5 = Table5 == null ? null : new { ValueID = (int)Table5.ValueId }, 
          table6 = Table6 == null ? null : new { StID = (int)Table6.StID, Col1 = (string)Table6.Col1 }, 
          table7 = Table7 == null ? null : new { ValueID = (int)Table7.ValueId } 
         }; 
+0

我试过这个解决方案,但它给了超时异常,我试图增加超时到2分钟,但仍然超时异常来了,而在Sql服务器,它只需要0.01秒。我想这是交叉加入LINQ的某处。 – Agalo

+0

您是否检查了linq生成的查询并与原始查询进行比较?对我来说工作得很好。也许你需要检查你的索引。 – Gandarez

0

尝试......它的工作对我来说很好..

var query = (from a in objEntity.tblStock 
        join b in objEntity.tblScript on a.ScriptId equals b.ScriptId 
        join c in (from x in objEntity.tblScript join y in objEntity.tblSector on x.SectorId equals y.SectorId select new{x.ScriptId, x.SectorId, y.SectorName}) on a.ScriptId equals c.ScriptId 
        join d in objEntity.tblShareholder on a.ShareholderId equals d.ShareholderId 
        select new 
         { 
          a.StockId, 
          b.ScriptId, 
          b.SectorId, 
          a.ShareholderId, 
          b.ScriptName, 
          c.SectorName, 
          d.Shareholdername, 
         }).ToList();