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

  • Home
  • SEARCH
  • 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 8367695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:13:28+00:00 2026-06-09T13:13:28+00:00

I am currently struggling to assemble a LINQ query on my objects collections :

  • 0

I am currently struggling to assemble a LINQ query on my objects collections : Persons, Cars

Each person can have multiple cars.

I want to select all PERSONS in persons and all group all cars owned by this person. The query I have written so far is:

from c in persons,d in cars 
    where c.id = d.ownerID 
    group by c.Firstname into MG = tolist() 

but it only returns persons who have cars. If a person does not have car, he is not in list. I cannot make up with the right logic.

  • 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-06-09T13:13:29+00:00Added an answer on June 9, 2026 at 1:13 pm

    try:

    List<person> plist = new List<person>();
            plist.Add(new person(1, "a"));
            plist.Add(new person(2, "b"));
            plist.Add(new person(3, "c"));
            plist.Add(new person(4, "d"));
    
            List<cars> clist = new List<cars>();
            clist.Add(new cars(1, "c1"));
            clist.Add(new cars(1, "c2"));
            clist.Add(new cars(1, "c5"));
            clist.Add(new cars(2, "c1"));
            clist.Add(new cars(3, "c1"));
            clist.Add(new cars(3, "c5"));
            clist.Add(new cars(3, "c3"));
            clist.Add(new cars(3, "c2"));
            clist.Add(new cars(4, "c2"));
            clist.Add(new cars(4, "c5"));
    
    
            var result = from p in plist
                    join c in clist on p.id equals c.ownerID into k
                    from s in k.DefaultIfEmpty()
                    select new { p.firstName , carName = (s == null ? String.Empty :s.name)};
    
    string sss = "";
     foreach (var v in result)
     {
          sss+= ( v.firstName + " : " + v.carName +  " >> "+"\n");
     }
     textBox1.Text = sss;
    

    and classes are :

    class person
    {
        public int id;
        public string firstName;
    
        public person(int id1, string name)
        {
            id = id1;
            firstName = name;
        }
    }
    
    class cars
    {
        public int ownerID;
        public string name;
    
       public cars(int id,string name1)
        {
            ownerID = id;
            name = name1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently struggling with a query that needs to retrieve multiple records from
I am currently struggling with HTTP Session replication on tomcat with complex objects. Some
I'm currently struggling to understand how I should organize/structure a class which I have
I am currently struggling with the following problem. I have successfully installed Apache2 and
I am currently struggling with a side project I hope you can help me
I am currently struggling with a problem, that I can't find the reason. Currently
I'm currently struggling to come up with a regex that can split up a
I hope someone can assist me with the problem I'm currently experiencing. We have
I am currently struggling to upload multiple files from the local storage to the
I am currently struggling with the GUI of my application. I have a hard

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.