简体   繁体   中英

Linq query to select data from table and join 2 tables

I am trying to select everything from a table and then make a join with two other tables, from which I need just some of the columns. The final list will be populated into a DevExpress GridControl . The main table that I take the data from has these columns:

Id | ScheduleId | AccountId | Description

I can easily select everything from this table and return. Here is my list:

public IList<StatusDescription> getStatusDescription()
{
    var listOfItems = from e in context.StatusDescription select e;
    return listOfItems.ToList<StatusDescription>();
}

The other two tables are Schedules and Accounts, they look as follows:

Id | ScheduleName | DateFrom | DateTo

and

Id | AccountName | AccountCode

I would like to join the DateFrom and DateTo from the Schedule table and the AccountCode from the Account table. Is it possible to get all that into a single list. I can easily bind it later to the GridControl

var listOfItems = from e in context.StatusDescription
                  join s in context.Schedules on e.ScheduleId equals s.Id
                  join a in context.Accounts on e.AccountId equals a.Id
                  select new 
                  {
                      Description = e.Description,
                      ScheduleStart = s.DateFrom,
                      ScheduleEnd = s.DateTo,
                      AccountCode = a.AccountCode
                  }

var Query = from p in context.StatusDescription select new { p.Id, p.Description, p.Schedule.DateFrom, p.Schedule.DateTo, p.Account.AccountCode };

希望能帮助到你

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