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 6630949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:28:49+00:00 2026-05-25T22:28:49+00:00

I need some help with Linq self-join. I have the following classs: public class

  • 0

I need some help with Linq self-join.

I have the following classs:

public class Owner
{
public string OwnerId {get;set;}
public string Name {get;set;}
public string Area {get;set;}
public string City {get;set;}
public string Sex {get;set;}
public List<Dog> dog {get;set;}
}

And table….

ID OwnerId OwnerName TypeId TypeName   TypeValue        TypeCodeId
1     1     John       1     Area      United States       440
2     1     John       2     City      Los-Angeles         221
3     1     John       3     Sex       Female              122
4     2     Mary       1     Area      Mexico              321
4     2     Mary       2     City      Cancun              345
............................................................

I need to parse results of table1 into list of owners the fastest way possible.
Note: Type can be null, but I still need to show that owner (So, I assume left join should work).

Here’s What I do. (owners is a webservice class that contains table1 results)

public IEnumerable<Owner> GetOwners() { 
  return  (from owner in owners 
          join area in owners into owner_area
          from oa in owner_area.DefaultIfEmpty()
          join City in owners into owner_city
          from oc in owner_city.DefaultIfEmpty()
          join sex  in owners into owner_sex
          from os in owner_sex.DefaultIfEmpty()
          where oa.TypeId == 1 && oc.TypeId ==2 && os.TypeId ==3
          select new Owner() {OwnerId = owner.OwnerId, 
                              Name = owner.Name,
                              Area = oa.TypeValue,
                              City = oc.TypeValue,
                              Sex = os.TypeValue}).Distinct(); 
}

This query has several issues:

  1. It returns multiple results and distinct does not work as expected
  2. I’ve tried to use GroupBy but it says that cannot implicitly convert Owner into IEnumerable <int, Owner>
  3. It is super slow

How can I get distinct record with self join and improve performance?
Thanks

UPDATE:
Thank you guys for your ansewers, testing now, but I figured out that I forgot to supply one more thing. I’ve added a new column called TypeCodeId to the table layout(see above)
User can filter values based on their selection. So, I have TypeCodeId + TypeValue dictionaries for Area, City and Sex. All of those parameters are optional (If the user didn’t select any, I just show them all records.

So, Assume that the user has selected filter Area: Unites States and filter City: Los Angeles

them my query would look like this:

Select Projects where Area equals United States(440) and City equals Los Angeles(221)

If Only Area:Mexico was selected then my query would read something like this:

Select Projects where Area equals Mexico(321)

I’m not sure how to do optional where clauses with what you’ve provided in the examples.

  • 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-25T22:28:50+00:00Added an answer on May 25, 2026 at 10:28 pm

    For best performance if think this is the way to do it.

    public IEnumerable<Owner> GetOwners(IEnumerable<Tuple<int, int>> filter)
    {
        var q = (from o in owners
                join f in filter on 
                   new {o.TypeId, o.TypeCodeId} equals 
                   new {TypeId = f.Item1, TypeCodeId = f.Item2}
                select o).ToList();
    
        var dic = q.ToDictionary (o => new {o.OwnerId, o.TypeId}, o => o.TypeValue);
        foreach (var o in q.Select(o => new { o.OwnerId, o.OwnerName }).Distinct())
        {
            var owner = new Owner()
            {
                OwnerId = o.OwnerId,
                Name = o.OwnerName
            };
            string lookup;
            if(dic.TryGetValue(new {o.OwnerId, TypeId = 1}, out lookup))
                owner.Area = lookup;
            if(dic.TryGetValue(new {o.OwnerId, TypeId = 2}, out lookup))
                owner.City = lookup;
            if(dic.TryGetValue(new {o.OwnerId, TypeId = 3}, out lookup))
                owner.Sex = lookup;
    
            yield return owner;
        }
    }
    

    To get even a little more performance you can write an IEqualityComparer class that only compares int OwnerId and send it into the Distinct function

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

Sidebar

Related Questions

I need some help with a LINQ query in VB.Net, please. I have this
Need some help about with Memcache. I have created a class and want to
Im trying to convert a SQL join to LINQ. I need some help in
I need some help with a linq query. Basically, I have a datatable with
I need some help adding conditions to a linq query. Take this small example
still new to the world of linq, and i need some help flatening a
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I need some help converting the SQL found below to an equivalent LINQ statement

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.