I cannot prepare a query that I need. I have code:
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();
}
}
and it doesn’t work as I expected. How do I prepare query that even if OwnerID in Dog object is null new instance of Group is still created and added to groups variable?
Like So
See http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/
Even though his examples show translation to SQL, the same applies for linq to objects