SELECT [t0].[Id],
[t0].[Challan_No],
[t0].[Type],
[t0].[Measurement],
[t0].[Update_By],
[t0].[Date],
[t1].[Id] AS [Id2],
[t1].[Challan_No] AS [Challan_No2],
[t1].[Own_Comany],
[t1].[Parti_Name],
[t1].[Update_By] AS [Update_By2],
[t1].[Date] AS [Date2],
[t1].[IsDelete],
[t1].[Isgenbill],
[t2].[Rate] AS [Rate1]
FROM [dbo].[Challan_Total] AS [t0]
INNER JOIN [dbo].[Challan] AS [t1] ON [t0].[Challan_No] = [t1].[Challan_No]
INNER JOIN [dbo].[Rate] AS [t2] ON [t0].[Type] = [t2].[Meterial_type]
AND [t1].Own_Comany = [t2].Company_Id
AND [t1].Parti_Name=[t2].Parti_Id
WHERE [t0].[Challan_No] = '2014CH 10002'
以下是我提出的建议:
var getch = dm.Challan_Totals.Where(m => m.Challan_No == "")
.Join(dm.Challans,
c => c.Challan_No, d => d.Challan_No,
(c, d) => new { c, d }).Select(b => b)
.Join(dm.Rates,
r => r.c.Type && r.d.Own_Comany && r.d.Parti_Name ,
s => s.Meterial_type && s.Company_Id && s.Parti_Id,
(r, s) => new { r, s.Rate1 });
答案 0 :(得分:3)
我认为这会给你你想要的东西:
var result = from t0 in ChallanTotal
join t1 in Challan on t0.Challan_No equals t1.Challan_No
join t2 in Rate on new { COL1 = t0.Type, COL2 = t1.Own_Comany, COL3 = t1.Parti_Name }
equals
new { COL1 = t2.Meterial_type, COL2 = t2.Company_Id, COL3 = t2.Parti_Id }
where t0.Challan_No == "2014CH 10002"
select new
{
Id = t0.Id,
Challan_no = t0.Challan_No,
Type = t0.Type,
Id2 = t1.Id,
Challan_No2 = t1.Challan_No,
Own_Company = t1.Own_Comany,
Rate1 = t2.Rate1
};
这是使用扩展方法的等效表达式:
var result = ChallanTotal.Join(Challan,
ct => ct.Challan_No ,
c => c.Challan_No,
(ct, c) => new { ct, c })
.Join(Rate,
ctc => new
{
Type = ctc.ct.Type,
Own_Company = ctc.c.Own_Comany,
Parti_Name = ctc.c.Parti_Name
},
rt => new
{
Type = rt.Meterial_type,
Own_Company = rt.Company_Id,
Parti_Name = rt.Parti_Id
},
(ctc, rt) => new {ctc, rt})
.Where(x => x.ctc.ct.Challan_No == "2014CH 10002")
.Select(res => new { Id = res.ctc.ct.Id,
Challan_no = res.ctc.ct.Challan_No,
Type = res.ctc.ct.Type,
Id2 = res.ctc.c.Id,
Challan_No2 = res.ctc.c.Challan_No,
Own_Company = res.ctc.c.Own_Comany,
Rate1 = res.rt.Rate1
});
加入多列时的关键点是:
P.S。我没有在上面的代码中包含所有列,但可以自己轻松添加。
修改强>
上面的查询已经过测试,可以使用以下模型运行:
类似于SQL Schema的类模型:
class Challan_Total
{
public int Id {get; set;}
public string Challan_No {get; set;}
public string Type {get; set;}
}
class Challan
{
public int Id { get; set; }
public string Challan_No {get; set;}
public string Own_Comany {get; set;}
public string Parti_Name {get; set;}
}
class Rate
{
public string Company_Id {get; set;}
public string Parti_Id {get; set;}
public string Meterial_type { get; set;}
public string Rate1 { get; set; }
}
示例数据:
List<Challan_Total> lstChallanTotal = new List<Challan_Total>()
{
new Challan_Total { Challan_No = "1", Id = 1, Type = "1"},
new Challan_Total { Challan_No = "2", Id = 1, Type = "1"},
new Challan_Total { Challan_No = "3", Id = 2, Type = "2"}
};
List<Challan> lstChallan = new List<Challan>()
{
new Challan { Id = 1, Challan_No = "1", Own_Comany = "1", Parti_Name = "1"},
new Challan { Id = 2, Challan_No = "2", Own_Comany = "1", Parti_Name = "2"},
new Challan { Id = 3, Challan_No = "3", Own_Comany = "3", Parti_Name = "3"}
};
List<Rate> lstRate = new List<Rate>()
{
new Rate { Company_Id = "1", Parti_Id = "1", Meterial_type = "1", Rate1 = "p1"},
new Rate { Company_Id = "2", Parti_Id = "1", Meterial_type = "1", Rate1 = "p2" },
new Rate { Company_Id = "3", Parti_Id = "2", Meterial_type = "2", Rate1 = "p3" }
};