Linq嵌套如果为null,则从组连接加入

时间:2013-09-04 01:41:53

标签: vb.net linq join

我确信这很简单,但对于我的生活,我找不到一个例子。

我正在桌面上进行群组加入,然后我想在该群组加入上进行第二次加入。

表1订单 表2联系人 表3电话号码

dim query = from order in Orders _
Group Join contact in Contacts On order.contactId equals contact.contactId Into grpContacts = Group From gcontact in grpContacts.DefaultIfEmpty()
Group Join phone in PhoneNumbers On phone.phoneNumberId Equals gcontact.homePhoneId Into grpPhoneNumbers = Group from gphone in grpPhoneNumbers.DefaultIfEmpty()

当gcontact为null时,此查询将在最后一次连接时出错。这是有道理的......但是如果记录不存在我怎么做这个加入并且只有null / nothing值?

更新已解决 * 如果其他人遇到此问题,您可以在ON运算符后添加表达式,以确定先前的组连接是否为空。谢谢你!

dim query = from order in Orders _
Group Join contact in Contacts On order.contactId equals contact.contactId Into grpContacts = Group From gcontact in grpContacts.DefaultIfEmpty()
Group Join phone in PhoneNumbers On IF(phone is nothing, 0, phone.phoneNumberId) Equals gcontact.homePhoneId Into grpPhoneNumbers = Group from gphone in grpPhoneNumbers.DefaultIfEmpty()

1 个答案:

答案 0 :(得分:2)

尝试使用iif避免在null时访问gcontact:

Equals iif(gcontact is nothing, 0, gcontact.homePhoneId)
相关问题