简体   繁体   中英

How to retrieve data from multiple tables using Entity Framework

I'm new to Entity Framework, and working on Boys & Girls Hostel Application. I decided to develop it as a WCF Service.

I have two tables

  1. Master

Tbl_Room

  • Rm_no int (PK)
  • Rm_Name varchar
  • RentPerBed money
  • SharingLevel int

    1. Child

Tbl_Bed

  • Sr_no (PK)
  • Rm_no int (FK)
  • Bed_no int
  • Status int

I wrote the following function to retrieve data for the available beds but I'm getting an error ...and I'm confused about how I can do this...

public IEnumerable<Tbl_Room> Available()
{
    var avilableRoomWithBed = (from r in db.Tbl_Room
                               join s in db.Tbl_Bed on r.Rm_No equals s.Rm_No
                               where s.Status == 0
                               select new Tbl_Room()
                               {
                                   Rm_No = r.Rm_No,
                                   Rm_Name = r.Rm_Name,
                                   Floar = r.Floar,
                                   RentPerBed = r.RentPerBed
                               }).ToList();

     return avilableRoomWithBed.ToList();
}

Here I want to retrieve Bed_no which have a status of 0 ie available

I am confused about the return type of IEnumerable and what I have to return

You are approaching your EF like SQL - if you've got your relationships set up in the class correctly (ie a navigation property called Beds on Tbl_Room) then you can do the following.

var avilableRoomWithBed = from r in db.Tbl_Room
                          where r.Beds.Any(b => b.Status == 0)
                          select r;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM