我无法准备我需要的查询。我有代码:linq查询加入
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();
}
}
并且它不能正常工作。即使在Dog对象中的OwnerID为空的情况下,我如何准备查询组的新实例仍然被创建并添加到组变量中?
非常感谢我想要的;) – TrN