Linq外连接查询

时间:2015-10-29 23:15:12

标签: vb.net linq

尝试在vb中使用linq将两个数据集连接在一起。 我的第一个数据集包含所有帐户的列表,而我的第二个数据集包含所有具有余额的帐户。 我想做一个连接,让我获得所有帐户,包括有余额的帐户。

这是我的代码

Dim results = From a In accounts
                      Group Join ba In balanceAccounts On c.AccountId Equals a.AccountId Into Group
                      From p In Group.DefaultIfEmpty()
                      Select New With {.Name = a.Name, .Balance = ba.Balance


                                                               }

3 个答案:

答案 0 :(得分:0)

没有加入:

balanceAccounts
    .Select(x => new Result() { Name = x.Name, Balance = x.Balance })
    .Union(accounts.Select(x => new Result() { Name = x.Name, Balance = x.Balance }))
    .GroupBy(x => x.Name)
    .Select(x => x.First())
    .ToList();

抱歉c#; - )

答案 1 :(得分:0)

在您的代码中,更改此

From p In Group.DefaultIfEmpty()

From ba In Group.DefaultIfEmpty()

.Balance = ba.Balance.Balance = p.Balance

答案 2 :(得分:0)

Dim results = From a In accounts
              Group Join ba In balanceAccounts On c.AccountId Equals a.AccountId Into Group
              From ba In Group.DefaultIfEmpty()
              Select New With 
              { .Name = a.Name, .Balance = ba.Balance }

http://blogs.msdn.com/b/vbteam/archive/2008/01/31/converting-sql-to-linq-part-8-left-right-outer-join-bill-horst.aspx

HF