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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:40:31+00:00 2026-06-12T16:40:31+00:00

I have a Address class. public class Address : RootEntityBase { virtual public string

  • 0

I have a Address class.

public class Address : RootEntityBase
{
    virtual public string Province { set; get; }
    virtual public string City { set; get; }        
    virtual public string PostalCode { set; get; }
}

By this default values:

var myList = new List<Address>
             {
               new Address {Province = "P1", City = "C1", PostalCode = "A"},
               new Address {Province = "P1", City = "C1", PostalCode = "B"},
               new Address {Province = "P1", City = "C1", PostalCode = "C"},

               new Address {Province = "P1", City = "C2", PostalCode = "D"},
               new Address {Province = "P1", City = "C2", PostalCode = "E"},

               new Address {Province = "P2", City = "C3", PostalCode = "F"},
               new Address {Province = "P2", City = "C3", PostalCode = "G"},
               new Address {Province = "P2", City = "C3", PostalCode = "H"},

               new Address {Province = "P2", City = "C4", PostalCode = "I"}
             };

I need to extraction distinct of this myList by two columns : Province & City

Namely similar to myExpertResult :

var myExpertResult = new List<Address>
                        {
                           new Address {Province = "P1", City = "C1"},
                           new Address {Province = "P1", City = "C2"},
                           new Address {Province = "P2", City = "C3"},
                           new Address {Province = "P2", City = "C4"}
                        }; 

So I use this code :

var list = myList.Select(x => new Address {City = x.City, Province = x.Province}).Distinct().ToList();

but my result is not valid because count of result is 9 namely all addresses.

Quivalent query in SQL is : select distinct Province , City from tblAddress

also I tested this query by linq to NHibernate.

var q = SessionInstance.Query<Address>();
        .Select(x => new Address { Province = x.Province, City = x.City }).Distinct().ToList();

But it is not support this query. Message of exception is : Expression type 'NhDistinctExpression' is not supported by this SelectClauseVisitor.

How can I do it?

  • 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-12T16:40:32+00:00Added an answer on June 12, 2026 at 4:40 pm

    You can use GroupBy:

    var result = myList.GroupBy(a => new { a.Province, a.City })
          .Select(g => new Address { 
                      Province = g.Key.Province, 
                      City = g.Key.City 
                  });
    

    Or use anonymous type:

     myList.Select(a => new { 
                Province = a.Province,
                City = a.City
            })
          .Distinct();
    

    By default, anonymous type uses value quality to compare, it is equivalent when all properties are equivalent.

    Another way is to customer EqualityComparer which uses Province and City for Equal and GetHashCode method with another overload Distinct in here

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

Sidebar

Related Questions

I have the following class: class Address { public string City {get; set;} public
I have this class public class Address:Entity { public virtual string Address1 { get;
I have the following: public class Address { public string Email { get; set;
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
i have this Class public Class Employee { String name; List<Address> listOfAddress; } public
I have a class at server-side like this: public class Address { public Address(string
I have the following two classes: public class Address { public string AddressLine1 {
I have a class structure that looks like this: class Person { public virtual
I have the following class: public class Address { public virtual int Id {
Say you have the following object: public class Address { public String Line1 {

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.