多个 Linq(扩展方法)与匿名类型连接
Multiple Linq (Extension Method) Joins With Anonymous Types
在 C# 中,我正在尝试执行多个扩展方法联接,其中第二个联接与第一个联接生成的匿名类型的枚举:
List<Contact> filteredContactList = GetFullContacts()
.Join(GetContactCompanyRoles()
, ct => ct.IdContact
, ctCmpRole => ctCmpRole.IdContact
, (ct, ctCmpRole) => new { Contact = ct, ContactType = ctCmpRole.ContactType })
.Join(GetContactRoles()
, ctf => new { ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ctRole.ContactId, ctRole.ContactType }
, (ctf, ctRole) => new { Contact = ctf.Contact, PrimaryInd = ctRole.IsPrimary})
.Select(rec => rec.Contact)
.ToList();
ct 和 ctf.Contact 是接触类型。
但是,我在尝试编译时遇到以下错误:
The type arguments for method 'System.Linq.Enumerable.Join...' cannot be inferred from the usage. Try specifying the type argument explicitly.
有没有办法避免这个错误,而不必为第一次连接的匿名类型创建一个实际的 class?还有其他我没有考虑的选择吗?
我注意到这里有一个明显的印刷错误:
, ctRole => new { ctRole.IdContact, ctRole.ConatactType }
也许你的意思是:
, ctRole => new { ctRole.IdContact, ctRole.ContactType }
即使用 ContactType 而不是 ConatactType。
解决此问题后,通过评论/聊天我们确定编译器错误仍在发生,因为一些用于设置多键连接的匿名 类 没有相同的成员名称。换句话说,而不是:
, ctf => new { ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ctRole.ContactId, ctRole.ContactType }
需要在两个匿名之间规范化成员名称类:
, ctf => new { ContactID = ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ContactID = ctRole.ContactId, ctRole.ContactType }
在 Vincent Ugenti 的帮助下,确定由于 属性 名称不匹配,编译器无法评估第二个连接的连接子句参数中的两个匿名类型。添加时
ContactID =
对于两个匿名初始值设定项,代码都能够编译。
.Join(GetContactRoles()
, ctf => new { ContactID = ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ContactID = ctRole.ContactId, ctRole.ContactType }
, (ctf, ctRole) => new { Contact = ctf.Contact, PrimaryInd = ctRole.IsPrimary})
在 C# 中,我正在尝试执行多个扩展方法联接,其中第二个联接与第一个联接生成的匿名类型的枚举:
List<Contact> filteredContactList = GetFullContacts()
.Join(GetContactCompanyRoles()
, ct => ct.IdContact
, ctCmpRole => ctCmpRole.IdContact
, (ct, ctCmpRole) => new { Contact = ct, ContactType = ctCmpRole.ContactType })
.Join(GetContactRoles()
, ctf => new { ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ctRole.ContactId, ctRole.ContactType }
, (ctf, ctRole) => new { Contact = ctf.Contact, PrimaryInd = ctRole.IsPrimary})
.Select(rec => rec.Contact)
.ToList();
ct 和 ctf.Contact 是接触类型。
但是,我在尝试编译时遇到以下错误:
The type arguments for method 'System.Linq.Enumerable.Join...' cannot be inferred from the usage. Try specifying the type argument explicitly.
有没有办法避免这个错误,而不必为第一次连接的匿名类型创建一个实际的 class?还有其他我没有考虑的选择吗?
我注意到这里有一个明显的印刷错误:
, ctRole => new { ctRole.IdContact, ctRole.ConatactType }
也许你的意思是:
, ctRole => new { ctRole.IdContact, ctRole.ContactType }
即使用 ContactType 而不是 ConatactType。
解决此问题后,通过评论/聊天我们确定编译器错误仍在发生,因为一些用于设置多键连接的匿名 类 没有相同的成员名称。换句话说,而不是:
, ctf => new { ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ctRole.ContactId, ctRole.ContactType }
需要在两个匿名之间规范化成员名称类:
, ctf => new { ContactID = ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ContactID = ctRole.ContactId, ctRole.ContactType }
在 Vincent Ugenti 的帮助下,确定由于 属性 名称不匹配,编译器无法评估第二个连接的连接子句参数中的两个匿名类型。添加时
ContactID =
对于两个匿名初始值设定项,代码都能够编译。
.Join(GetContactRoles()
, ctf => new { ContactID = ctf.Contact.IdContact, ctf.ContactType }
, ctRole => new { ContactID = ctRole.ContactId, ctRole.ContactType }
, (ctf, ctRole) => new { Contact = ctf.Contact, PrimaryInd = ctRole.IsPrimary})