2015-05-19 61 views
1

你好。我想加盟4个表:LINQ内部连接4个表(将SQL转换为C#LINQ)

tStore(STOREID,STORE_NAME)

tSection(SectionID,SECTION_NAME)

tSectionSqft(SectionSqftID,STOREID,SectionID,平方英尺)

tSectionForwardSelling(SectionForwardSellingID ,店号,节号,金额,日期)

我想查询给我结果:

STORE_NAME,SECTION_NAME,平米,金额

我需要这个SQL查询转换为C#LINQ:

SELECT 
    tStore.Store_Name, 
    tSection.Section_Name, 
    tSectionSqft.Sqft, 
    tSectionForwardSelling.Amount 
FROM tSection 
INNER JOIN tSectionForwardSelling 
     ON tSection.SectionID = tSectionForwardSelling.SectionID 
INNER JOIN tSectionSqft 
     ON tSection.SectionID = tSectionSqft.SectionID 
INNER JOIN tStore 
     ON tSectionForwardSelling.StoreID = tStore.StoreID 
     AND tSectionSqft.StoreID = tStore.StoreID 

我想对我自己的,但每次LINQ给了我错误的结果。

+2

您是否使用实体框架? –

+2

你已经试过了什么,你看到了什么错误的结果? – Hammerstein

+0

是的。它是Asp.net MVC项目。 – Timur

回答

0

你应该做这样的事情(只是一个片段):

var result = from s in tSection join ss in tSectionForwardSelling on s.SectionId equals ss.sectionID 
     join sq in tSectionSqft on s.SectonId equals sq.SectionID 
     join st in stores on ...... 
     select new {field1 = s.Field1 
        field2 = sq.FieldName,...} 
2
var queryResult = from a in tSection 
      join b in tSectionForwardSelling on a.SectionID equals b.SectionID 
      join c in tSectionSqft on a.SectionID equals c.SectionID 
      join d in tStore on new { u1 = b.SectionID , u2 = c.SectionID } equals new { u1 = d.SectionID , u2 = d.SectionID } 
      select new { d.Store_Name, a.Section_Name, c.Sqft, b.Amount }; 
+0

www.dropbox.com/s/qbb7qz3shbkuot2/pic.png?dl=0它应该工作 – Timur

+0

Link是不可行 – Nazmul

+0

https://drive.google.com/file/d/0BwgF9RnNTDDEVGJMMTdmMlh5VzA/view?usp=sharing 链接谷歌驱动器 – Timur

0

试试这个:

public class RetrieveData 
{ 
    public string StoreName { get; set; } 
    public string SectionName { get; set; } 
    public int Sqft { get; set; } 
    public int Amount { get; set; } 
} 

public void Method(tStore store) 
{ 
    // Store_Name, Section_Name, Sqft, Amount 

    var list = from se in Context.Sections 
       join fs in tSectionForwardSelling on se.SectionID equals fs.SectionID 
       join sqft in tSectionSqft on se.SectionID equals sqft.SectionID 
       join st in tStore on new { u1 = fs.StoreID , u2 = st.StoreID } equals new { u1 = sqft.StoreID , u2 = st.StoreID } 
       select new RetrieveData() { 
        StoreName = st.Store_Name, 
        SectionName = se.Section_Name, 
        Sqft = sqft.Sqft, 
        Amount = fs.Amount }; 
} 

我想给join到@Nazmul的一部分功劳。

+0

https://drive.google.com/file/d/0BwgF9RnNTDDEa0FTNloxWjVGaGxuRWRWT1diNGN2RTdwSzBB/view?usp=sharing tStore有许多与tSection通过桥表tSectionSqft和tSectioForwardSelling – Timur

+0

我会编辑它。稍等片刻。 –

+0

这些是我的模型: https://drive.google.com/file/d/0BwgF9RnNTDDEV3ZGS2xRamo2OFhsS05pQ3oyck9UNkx0MFMw/view?usp=sharing – Timur