How can i write a inner join query using linq

2019-06-11 14:15发布

问题:

I have write a inner join query. In SQL Server it is working fine, but I want to write this query using Linq-to-SQL. Can anybody help me please?

Here is my query in SQL Server:

select db.Firstname, db.lastname, lo.BiPayAmt, lo.Loan_ID, sc.Pmt_Id,
    sc.Terms, sc.pmt_dates, sc.LoanId
from dbo.Clients db
inner join dbo.Loans lo on db.Client_ID = lo.Client_ID
inner join dbo.SchedulePayments sc on lo.Loan_ID = sc.LoanId

回答1:

Roughly this:

from db in dbo.Clients
join lo in dbo.Loans on db.Client_ID equals lo.Client_ID
join sc in dbo.SchedulePayments on lo.Loan_ID equals sc.LoanId
select new
{
    db.Firstname,
    db.lastname,
    lo.BiPayAmt,
    lo.Loan_ID,
    sc.Pmt_Id,
    sc.Terms,
    sc.pmt_dates,
    sc.LoanId
}