LINQ to DataSet多表左连接

时间:2017-10-17 14:24:19

标签: c# linq join

有许多地方展示了如何使用LINQ将两个表连接起来,但是我在添加更多连接时遇到了麻烦(我有15个表)。我以为我的模式已经下降,但我收到了错误。顺便说一句,这是针对强类型DataSet的,尽管我并不怀疑这一点。虽然我实际上有更多的表要添加到连接中,但我试图让它最初使用较少的连接(星号是发生错误的地方):

var results = 
    from e in DataSetHelper.ds.Employee
    join es in DataSetHelper.ds.EmployeeSkill on e.EmployeeId equals es.EmployeeId    into esGroup from esItem in esGroup.DefaultIfEmpty()
    join s in DataSetHelper.ds.Skill on **es.SkillId** equals s.SkillId                   into skillGroup from skillItem in skillGroup.DefaultIfEmpty()
    join er in DataSetHelper.ds.EmployeeRole on e.EmployeeId equals er.EmployeeId     into erGroup from erItem in erGroup.DefaultIfEmpty()
    join r in DataSetHelper.ds.Role on **er.RoleId** equals r.RoleId                      into rGroup from rItem in rGroup.DefaultIfEmpty()

我遇到两个错误(同样的事情,但在不同的连接上)。它们位于查询的第3行和第5行。

对于es.SkillId,,错误为The name 'es' does not exist in the current context.

对于er.RoleId,,错误为The name 'er' does not exist in the current context.

同样,我需要将此模式用于另外十个连接,所以我希望这种模式不会随着我的复杂性而增加。

2 个答案:

答案 0 :(得分:0)

试试这个

var results = DataSetHelper.ds.Employee
.Join(DataSetHelper.ds.EmployeeSkill, e => e.EmployeeId, es => es.EmployeeId, (e, es) => new { e, es })
.Join(DataSetHelper.ds.Skill, esGroup  => esGroup.es.SkillId, s => s.SkillId, (esGroup, s) => new { esGroup.e, esGroup.es, s })
.Join(....

我已经展示了直到第一个多对多的关系。对于一切都会有点长,但我希望你能管理它。

答案 1 :(得分:0)

这很有效。我还必须考虑连接列上是否存在null的可能性。我使用了空条件运算符。不幸的是,使用内存中的数据集与使用SQL Server一样慢(我甚至没有完成所有连接或所有where子句测试)。好像我需要使用不同的方法。

var results = from e in _data.ds.Employee
              join es in _data.ds.EmployeeSkill on e.EmployeeId equals es.EmployeeId        into esGroup from esItem in esGroup.DefaultIfEmpty()
              join s in _data.ds.Skill on esItem?.SkillId equals s.SkillId                  into sGroup from skillItem in sGroup.DefaultIfEmpty()
              join er in _data.ds.EmployeeRole on e.EmployeeId equals er.EmployeeId         into erGroup from erItem in erGroup.DefaultIfEmpty()
              join r in _data.ds.Role on erItem?.RoleId equals r.RoleId                     into rGroup from rItem in rGroup.DefaultIfEmpty()
              join et in _data.ds.EmployeeTechnology on e.EmployeeId equals et.EmployeeId   into etGroup from etItem in etGroup.DefaultIfEmpty()
              join t in _data.ds.Technology on etItem?.TechnologyId equals t.TechnologyId   into tGroup from tItem in etGroup.DefaultIfEmpty()
              where
                e.FirstName.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
                e.LastName.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
                e.RMMarket.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 ||
                !e.IsSummaryNull() && e.Summary.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0
              select new SearchResult
              {
                  EmployeeId = e.EmployeeId,
                  Name = e.FirstName + " " + e.LastName,
                  Title = e.Title,
                  ImageUrl = e.IsImageUrlNull() ? string.Empty : e.ImageUrl,
                  Market = e.RMMarket,
                  Group = e.Group,
                  Summary = e.IsSummaryNull() ? string.Empty : e.Summary.Substring(1, e.Summary.Length < summaryLength ? e.Summary.Length - 1 : summaryLength),
                  AdUserName = e.AdUserName
              };