[英]LINQ inner join 4 tables (convert SQL to C# LINQ)
Hello. 你好。 I am trying to join 4 tables:
我正在尝试加入4个桌子:
tStore(StoreID, Store_Name) tStore(StoreID,Store_Name)
tSection(SectionID, Section_Name) tSection(SectionID,Section_Name)
tSectionSqft(SectionSqftID, StoreID, SectionID,Sqft) tSectionSqft(SectionSqftID,StoreID,SectionID,Sqft)
tSectionForwardSelling(SectionForwardSellingID, StoreID, SectionID, Amount, Date) tSectionForwardSelling(SectionForwardSellingID,StoreID,SectionID,金额,日期)
I want the query to give me results: 我希望查询给我结果:
Store_Name, Section_Name, Sqft, Amount 商店名称,科目名称,平方英尺,金额
I need to convert this SQL query to C# LINQ: 我需要将此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
I tried on my own but each time LINQ gives me wrong results. 我自己尝试,但是每次LINQ给我错误的结果。
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 };
You should do something like this (just a snippet): 您应该执行以下操作(只是一个片段):
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,...}
Try this: 尝试这个:
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 };
}
I want to give part credit of the join
to @Nazmul. 我想部分感谢@Nazmul的
join
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.