LINQ连接和GroupJoin

LINQ连接和GroupJoin,linq,linq-to-sql,Linq,Linq To Sql,我有一个对象列表,这些对象可能有也可能没有联系人信息: // Join contact query = query.Join( (new ContactRepository(this.Db)).List().Where(x => x.IsMainContact), x => new { x.ListItem.ContentObject.LoginId }, y => new { y.Logi

我有一个对象列表,这些对象可能有也可能没有联系人信息:

            // Join contact
        query = query.Join(
        (new ContactRepository(this.Db)).List().Where(x => x.IsMainContact),
        x => new { x.ListItem.ContentObject.LoginId },
        y => new { y.LoginId },
        (x, y) => new ListItemExtended<ListItemFirm>
        {
            City = y.City,
            State = y.State,
            Country = y.Country
        });
//加入联系人
query=query.Join(
(new ContactRepository(this.Db)).List()。其中(x=>x.IsMainContact),
x=>new{x.ListItem.ContentObject.LoginId},
y=>new{y.LoginId},
(x,y)=>新列表项扩展
{
城市,
State=y.State,
国家
});
这不会在“LoginId”上进行内部联接。但是我需要一个outter加入,这样如果给定LoginId的联系人信息不存在,它将是空的。 请帮忙


谢谢

您应该手动执行外部联接:

            var contacts = (new ContactRepository(this.Db)).List();
            query.Select(item => 
            {
                var foundContact = contacts.FirstOrDefault(contact => contact.Id == item.Id);
                return  new ListItemExtended<ListItemFirm>()
                    {
                        Id = item.Id,
                        City = foundContact != null ? foundContact.City : null,
                        State = foundContact != null ? foundContact.State : null,
                        Country = foundContact != null ? foundContact.Country : null,
                    };
            })
var contacts=(新ContactRepository(this.Db)).List();
query.Select(项=>
{
var foundContact=contacts.FirstOrDefault(contact=>contact.Id==item.Id);
返回新的ListItemExtended()
{
Id=项目Id,
City=foundContact!=null?foundContact.City:null,
State=foundContact!=null?foundContact。State:null,
Country=foundContact!=null?foundContact.国家:null,
};
})

但请记住,如果您的联系人项目是struct,则检查null不是正确的方法。使用Any()运算符代替FirstOrDefault()

您应该手动执行外部联接:

            var contacts = (new ContactRepository(this.Db)).List();
            query.Select(item => 
            {
                var foundContact = contacts.FirstOrDefault(contact => contact.Id == item.Id);
                return  new ListItemExtended<ListItemFirm>()
                    {
                        Id = item.Id,
                        City = foundContact != null ? foundContact.City : null,
                        State = foundContact != null ? foundContact.State : null,
                        Country = foundContact != null ? foundContact.Country : null,
                    };
            })
var contacts=(新ContactRepository(this.Db)).List();
query.Select(项=>
{
var foundContact=contacts.FirstOrDefault(contact=>contact.Id==item.Id);
返回新的ListItemExtended()
{
Id=项目Id,
City=foundContact!=null?foundContact.City:null,
State=foundContact!=null?foundContact。State:null,
Country=foundContact!=null?foundContact.国家:null,
};
})
但请记住,如果您的联系人项目是struct,则检查null不是正确的方法。使用Any()运算符代替FirstOrDefault()