简体   繁体   中英

How to get data only relating to the session of the staff members StaffID

I have figured out that to get the staff members from a company I use the following:

public ActionResult Index()
{
    int staffID = (int)Session["StaffID"];
    var staffRecord = db.Staffs.FirstOrDefault(staff => staff.StaffID == staffID);
    var company = staffRecord.Company;
    var staffForCompany = company.Staffs;
    return View(staffForCompany);
}

and this gets the bookings for that staff/company:

public ActionResult Index()
{
    int staffID = (int)Session["StaffID"];
    var staffRecord = db.Staffs.FirstOrDefault(staff => staff.StaffID == staffID);
    var company = staffRecord.Company;
    var bookingsForCompany = company.Bookings;
    return View(bookingsForCompany);
}

What I am wanting to know is how do I get data from a table that is not directly associated with the staff/company tables.

I am wanting the Customers that relate to the company of the staff member logged in.

See image here http://www.adamoxenhamsmith.co.uk/Uploads/download.jpg

From the ER diagram given in the image, it seems Company has 1..n bookings and each booking is tied to 1 Customer

This should work:

var customers = staffRecord.Company.Bookings.Select(b => b.Customer);

This should work:

var customers = db.Staffs
                  .Where(s => s.StaffID == staffId)
                  .Select(s => s.Company)
                  .SelectMany(c => c.Bookings)
                  .Select(bk => bk.Customer)
                  .ToList();

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