Apostrofix
Apostrofix

Reputation: 2180

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

Upvotes: 0

Views: 8583

Answers (2)

Nima Derakhshanjan
Nima Derakhshanjan

Reputation: 1404

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

hope it helps

Upvotes: 0

James
James

Reputation: 82096

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
                  }

Upvotes: 3

Related Questions