How to use join using lambda expression in Asp MVC

2019-09-13 21:16发布

I saw many articles on join in lambda expression format but i got an error in join.

I have two table one is tbl_payment and another is tbl_bill.
tbl_bank has ID(Primary Key), BILL_TYPE.
tbl_payment has ID(Primary Key),AMT,BILL_ID(Foreign Key of tbl_bank).

I want to get all data form tbl_payment and bill_type from tbl_bill in one query result.

Code :

ViewBag.requestHistory =
               db.tbl_bill.Join(db.tbl_payment, x => x.ID, y => y.BILL_ID,
    (x, y) => new {x.BILL_TYPE,y.ID,y.AMT,y.COMMENT,y.PAYMENT_METHOD,y.STATUS}).ToList();

It's gave an error :

The type arguments for method `Querable.Join<TOuter,.....> cannot be infrred from the usage`.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-13 21:48

I prefer to use the verbose linq syntax for joins

var requestHistory = 
    from bill in db.tbl_bill
    join payment in db.tbl_payment on bill.ID equals payment.BILL_ID
    select new {bill.BILL_TYPE, payment.ID, etc}
查看更多
登录 后发表回答