2015-05-19 68 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