2016-11-17 27 views
0

连接多个表我有2个表:左使用lambda表达

规格{规范ID,内容描述,createdby,lastupdatedby}
用户{用户ID,用户名}

我想要的下面LINQ查询需要写入纯lambda表达式

from spec in specs 
    from user in users.where(x => x.userId== spec.createdby).DefaultIfEmpty() 
    from updatedUser in users.where(x => x.userId== spec.lastupdatedbyby).DefaultIfEmpty() 
select new { 
spec = spec 
user = user, 
updatedUser = updatedUser 
} 

请协助。

数据会是这样说的:

spec[{1, test, 1234, 2345},{2, test1, 1234, null}] 

users[{1234, Allen},{2345, Dwayne}] 

所以结果应该是

[{1, test, Allen, Dwayne}, {2, test1, Allen, null}] 
+0

你能分享服务,您希望得到如表? – Eldeniz

+0

@Eldeniz我刚刚更新了我的问题样本数据和结果。 –

+0

@RustinCohle请检查我的回答 – user449689

回答

1

让我们先从这些类:

class Specs { 
    public int specId { get; set; } 
    public string desc { get; set; } 
    public int createdby { get; set; } 
    public int lastupdatedby { get; set; } 
} 

class Users { 
    public int userId { get; set; } 
    public string username { get; set; } 
} 

class UpdatedUser { 
    public int userId {get; set;} 
    public string username { get; set; } 
} 

现在LINQ查询,为方便起见,我创建了一些示例数据:

var specs = new Specs[] 
{ 
    new Specs{specId = 1, desc = "Spec1", createdby=1, lastupdatedby=1}, 
    new Specs{specId = 2, desc = "Spec2", createdby=2, lastupdatedby=3},  
    new Specs{specId = 3, desc = "Spec3", createdby=3, lastupdatedby=1}, 
    new Specs{specId = 4, desc = "Spec4", createdby=3, lastupdatedby=3}, 
}; 

var user = new Users[] 
{ 
    new Users{userId = 1, username = "User1"}, 
    new Users{userId = 2, username = "User2"}, 
}; 

var updatedUser = new UpdatedUser[] 
{ 
    new UpdatedUser{userId = 1, username = "UpdatedUser1"}, 
    new UpdatedUser{userId = 2, username = "UpdatedUser2"},   
}; 

var result = specs 
    .GroupJoin(user, 
     s => s.createdby, 
     u => u.userId, 
    (s, u) => u.Select(x => new {spec = s, user = x}) 
      .DefaultIfEmpty(new {spec = s, user = (Users)null})) 
.SelectMany(g => g) 
.GroupJoin(updatedUser, 
     firstJoin => firstJoin.spec.lastupdatedby, 
     uu => uu.userId, 
     (firstJoin, uu) => 
     uu.Select(y => new {spec = firstJoin.spec, user = firstJoin.user, updatedUser = y}) 
.DefaultIfEmpty(new {spec = firstJoin.spec, user = firstJoin.user, updatedUser = (UpdatedUser) null})) 
    .SelectMany(g1 => g1) 
    .ToList(); 

GroupJoin扩展方法帮助您获得一个元组出发表与连接表的元素的列表中的所有元素。

现在,如果你列举的结果:

result.ForEach(item => { 
    Console.WriteLine(item.spec.desc); 
    Console.WriteLine(item.user != null ? item.user.username : "NULL"); 
    Console.WriteLine(item.updatedUser != null ? item.updatedUser.username : "NULL"); 
    Console.WriteLine(); 
}); 

您获得此:

Spec1 
User1 
UpdatedUser1 

Spec2 
User2 
NULL 

Spec3 
NULL 
UpdatedUser1 

Spec4 
NULL 
NULL 
0

您可以尝试

var list = specs.Join(users, 
       s => s.lastupdatedby, 
       u => u.userid, 
       (s, u) => new { specs = s, users = u }) 
       .Select(x => new { 
        specId = x.specs.specId, 
        desc = x.specs.desc, 
        createdby=x.specs.createdby, 
        username=x.users.username 

       }).ToString();