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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:12:33+00:00 2026-05-12T06:12:33+00:00

I have a class like this: class MyClass<T> { public string value1 { get;

  • 0

I have a class like this:

class MyClass<T> {
    public string value1 { get; set; }
    public T objT { get; set; }
}

and a list of this class. I would like to use .net 3.5 lambda or linq to get a list of MyClass by distinct value1. I guess this is possible and much simpler than the way in .net 2.0 to cache a list like this:

List<MyClass<T>> list; 
...
List<MyClass<T>> listDistinct = new List<MyClass<T>>();
foreach (MyClass<T> instance in list)
{
    // some code to check if listDistinct does contain obj with intance.Value1
    // then listDistinct.Add(instance);
}

What is the lambda or LINQ way to 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-05-12T06:12:33+00:00Added an answer on May 12, 2026 at 6:12 am

    Both Marc‘s and dahlbyk‘s answers seem to work very well. I have a much simpler solution though. Instead of using Distinct, you can use GroupBy. It goes like this:

    var listDistinct
        = list.GroupBy(
            i => i.value1,
            (key, group) => group.First()
        ).ToArray();
    

    Notice that I’ve passed two functions to the GroupBy(). The first is a key selector. The second gets only one item from each group. From your question, I assumed First() was the right one. You can write a different one, if you want to. You can try Last() to see what I mean.

    I ran a test with the following input:

    var list = new [] {
        new { value1 = "ABC", objT = 0 },
        new { value1 = "ABC", objT = 1 },
        new { value1 = "123", objT = 2 },
        new { value1 = "123", objT = 3 },
        new { value1 = "FOO", objT = 4 },
        new { value1 = "BAR", objT = 5 },
        new { value1 = "BAR", objT = 6 },
        new { value1 = "BAR", objT = 7 },
        new { value1 = "UGH", objT = 8 },
    };
    

    The result was:

    //{ value1 = ABC, objT = 0 }
    //{ value1 = 123, objT = 2 }
    //{ value1 = FOO, objT = 4 }
    //{ value1 = BAR, objT = 5 }
    //{ value1 = UGH, objT = 8 }
    

    I haven’t tested it for performance. I believe that this solution is probably a little bit slower than one that uses Distinct. Despite this disadvantage, there are two great advantages: simplicity and flexibility. Usually, it’s better to favor simplicity over optimization, but it really depends on the problem you’re trying to solve.

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

Sidebar

Ask A Question

Stats

  • Questions 138k
  • Answers 138k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to keep two URL's apart: the one you… May 12, 2026 at 7:29 am
  • Editorial Team
    Editorial Team added an answer Well if you mean that 2 directories are on the… May 12, 2026 at 7:29 am
  • Editorial Team
    Editorial Team added an answer What you're looking for is bitmap/raster image to vector software.… May 12, 2026 at 7:29 am

Related Questions

I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have a class which looks something like this: class MyClass { public: //
I have a template class defined in a header file like this. Here I
I've got a problem with inheritance and generics. This is the code that illustrates
I have a class that acts like this at the moment: public class MyClass

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.