C# 带连接的linq查询

C# 带连接的linq查询,c#,.net,linq,collections,C#,.net,Linq,Collections,我无法准备我需要的查询。我有密码: public class Dog { public int id; public int? OwnerID; public string name; } public class Person { public int id; public string Name; } public class Group { public Dog dog; public Person owner; } class

我无法准备我需要的查询。我有密码:

public class Dog
{
    public int id;
    public int? OwnerID;
    public string name;
}

public class Person
{
    public int id;
    public string Name; 
}

public class Group
{
    public Dog dog;
    public Person owner;
}
class Program
{
    static void Main(string[] args)
    {
        IEnumerable<Dog> dogs = new[] { 
                                        new Dog { id = 1, OwnerID = null, name = "Burke" }, 
                                        new Dog { id = 2, OwnerID = 2, name = "Barkley" } 
                                      };
        IEnumerable<Person> owners = new[] { 
                                               new Person { id = 1, Name = "Jack" },
                                               new Person { id = 2, Name = "Philip" }
                                            };

        var groups = from dog in dogs
                     join owner in owners on dog.OwnerID equals owner.id
                     select new Group
                     {
                         dog = dogs.First(d => d.id == dog.id),
                         owner = owners.First(o => o.id == owner.id)
                     };
        foreach (var g in groups)
        {
            var text = g.dog.name + " belongs to " + (g.owner == null ? "no one" : g.owner.Name);
            Console.WriteLine(text);
        }
        Console.ReadLine();
    }
}
公共级狗
{
公共int id;
公共内部所有者ID;
公共字符串名称;
}
公共阶层人士
{
公共int id;
公共字符串名称;
}
公共课组
{
公犬;
公众人物所有者;
}
班级计划
{
静态void Main(字符串[]参数)
{
IEnumerable dogs=new[]{
新狗{id=1,OwnerID=null,name=“Burke”},
新狗{id=2,OwnerID=2,name=“Barkley”}
};
IEnumerable owners=new[]{
新人{id=1,Name=“Jack”},
新人{id=2,Name=“Philip”}
};
var组=来自狗中的狗
在dog.OwnerID等于owner.id上加入所有者
选择新组
{
dog=dogs.First(d=>d.id==dog.id),
owner=owner.First(o=>o.id==owner.id)
};
foreach(组中的变量g)
{
var text=g.dog.name+“属于”+(g.owner==null?“没有人”:g.owner.name);
控制台写入线(文本);
}
Console.ReadLine();
}
}
但它并没有像我预期的那样起作用。如何准备查询,即使Dog对象中的OwnerID为null,仍然会创建组的新实例并将其添加到groups变量中?

类似于此

var groups = from dog in dogs
join owner in owners on dog.OwnerID equals owner.id
from own in owner.DefaultIfEmpty()
select new Group
{
  dog = dogs.First(d => d.id == dog.id),
  owner = own.First(o => o.id == owner.id)
};

尽管他的示例显示了到SQL的转换,但同样的情况也适用于linq到对象

var groups = from dog in dogs
join owner in owners on dog.OwnerID equals owner.id
from own in owner.DefaultIfEmpty()
select new Group
{
  dog = dogs.First(d => d.id == dog.id),
  owner = own.First(o => o.id == owner.id)
};


尽管他的示例显示了到SQL的转换,但从您所描述的内容来看,这同样适用于linq to objects,这听起来像是要对Dogs执行左外连接查询。
查看Microsoft的LINQ示例,了解如何使用
DefaultIfEmpty()
LINQ函数。

根据您的描述,听起来您想对狗执行左外连接查询。
查看Microsoft的LINQ示例,了解如何使用
DefaultIfEmpty()
LINQ函数。

非常感谢这一点;)非常感谢,这正是我想要的;)