Linq查询以从表中选择数据并联接2个表 [英] Linq query to select data from table and join 2 tables

查看:63
本文介绍了Linq查询以从表中选择数据并联接2个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个表中的所有内容select,然后与其他两个表进行联接,从中我只需要其中的一些列.最终列表将填充到DevExpress GridControl中.我从中获取数据的主表具有以下列:

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

Id | AccountName | AccountCode

我想加入Schedule表中的DateFromDateTo以及Account表中的AccountCode.是否有可能将所有内容合并到一个列表中.我以后可以轻松地将其绑定到GridControl

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
                  }

这篇关于Linq查询以从表中选择数据并联接2个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆