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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:38:07+00:00 2026-05-16T05:38:07+00:00

I have a property on a class that is an ISet. I’m trying to

  • 0

I have a property on a class that is an ISet. I’m trying to get the results of a linq query into that property, but can’t figure out how to do so.

Basically, looking for the last part of this:

ISet<T> foo = new HashedSet<T>();
foo = (from x in bar.Items select x).SOMETHING;

Could also do this:

HashSet<T> foo = new HashSet<T>();
foo = (from x in bar.Items select x).SOMETHING;
  • 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-16T05:38:08+00:00Added an answer on May 16, 2026 at 5:38 am

    Edit (2023): There is now an ToHashSet extension method – see Douglas’ answer below.

    Original Answer:

    I don’t think there’s anything built in which does this… but it’s really easy to write an extension method:

    public static class Extensions
    {
        public static HashSet<T> ToHashSet<T>(
            this IEnumerable<T> source,
            IEqualityComparer<T> comparer = null)
        {
            return new HashSet<T>(source, comparer);
        }
    }
    

    Note that you really do want an extension method (or at least a generic method of some form) here, because you may not be able to express the type of T explicitly:

    var query = from i in Enumerable.Range(0, 10)
                select new { i, j = i + 1 };
    var resultSet = query.ToHashSet();
    

    You can’t do that with an explicit call to the HashSet<T> constructor. We’re relying on type inference for generic methods to do it for us.

    Now you could choose to name it ToSet and return ISet<T> – but I’d stick with ToHashSet and the concrete type. This is consistent with the standard LINQ operators (ToDictionary, ToList) and allows for future expansion (e.g. ToSortedSet). You may also want to provide an overload specifying the comparison to use.

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

Sidebar

Related Questions

I have a property on a class that happens to be a Table<T> .
I have a property inside a class that is getting changed by something. The
I want to have a class property that allow for an expression to take
D2.0 classes have a __monitor class property that gives access to the class object's
I have a class that has a property that I need to stub. I
I have a class that has the following property that is generated by the
I have a Customer class that contains a property, MyProperty, which is of a
I have a Data class that subclasses NSManagedObject with a single bool property attribute.
I have a Menu class that has a IQueryable property called WebPages. In the
I have a property within a class, that I need to iterate through all

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.