[英]LINQ inner join 4 tables (convert SQL to C# LINQ)
你好。 我正在尝试加入4个桌子:
tStore(StoreID,Store_Name)
tSection(SectionID,Section_Name)
tSectionSqft(SectionSqftID,StoreID,SectionID,Sqft)
tSectionForwardSelling(SectionForwardSellingID,StoreID,SectionID,金额,日期)
我希望查询给我结果:
商店名称,科目名称,平方英尺,金额
我需要将此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给我错误的结果。
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 };
您应该执行以下操作(只是一个片段):
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,...}
尝试这个:
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 };
}
我想部分感谢@Nazmul的join
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.