Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:41:32+00:00 2026-05-24T13:41:32+00:00

I cannot prepare a query that I need. I have code: public class Dog

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:41:34+00:00Added an answer on May 24, 2026 at 1:41 pm

    Like So

    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)
    };
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL query that goes like this: $stmt = $dbh->prepare(INSERT INTO Places
I cannot get a two-way bind in WPF to work. I have a string
I have a follwing class structure: public abstract class AbstractFoo { public virtual void
I have never done any Singleton class before and now I figured that for
Some people believe that mysql_real_escape_string() has some flaws and cannot protect your query even
I cannot understand how this is possible. Please help!! I have an app with
I cannot seem to debug my JavaScript code with Firebug. The play button is
I have been reading some recipes in the Perl Hacks book. Recipe #24 Query
I want to create a function that has a select query inside that can
I'm fetching data from external database (I cannot edit it so don't suggest that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.