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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:15:10+00:00 2026-05-15T14:15:10+00:00

I’ve been using LINQ for a while now, but seem to be stuck on

  • 0

I’ve been using LINQ for a while now, but seem to be stuck on something with regards to Unique items, I have the folling list:

List<Stock> stock = new List<Stock>();

This has the following Properties: string ID , string Type, string Description, example:

public class Stock
{
    public string ID { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
}

I want to have a LINQ query that will group the items in Stock by their Type and return this as a new List of Stock (it has to be the same type as the original Stock List).

Example Data:

 ID   Type                 Description
----------------------------------------------
  1   Kitchen Appliance    Dishwasher  
  2   Living Room          Television  
  3   Kitchen Appliance    Washing Machine  
  4   Kitchen Appliance    Fridge  

…

My Linq query wants to be able to return all the Kitchen Appliances for Example.
So I would pass this as a “type” into the query and it would return the items 1, 3 and 4
from this example list.

This list returned must also be of type: List<Stock>.

Essentially I want a list of the unique items by type, kind of like an SQL Distinct query, how do I achieve this in LINQ?
Alternative solutions are fine but must be Silverlight / C# client code only.

Just another clarification is that I also may not provide the parameter “Kitchen Appliance” and may just want the unique ones, for example It would return Kitchen Appliance and Living Room once each only to kind of like a category no matter how many of that Type there are.

  • 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-15T14:15:11+00:00Added an answer on May 15, 2026 at 2:15 pm

    Flexible approach

    Use GroupBy and ToDictionary to create a dictionary of List<Stock> values keyed on the Type property:

    var appliancesByType = stock
        .GroupBy(item => item.Type)
        .ToDictionary(grp => grp.Key, grp => grp.ToList());
    

    Then you can access the types themselves as well as a list for any given type quite easily:

    // List of unique type names only
    List<string> stockTypes = appliancesByType.Keys.ToList();
    
    // Or: list of one stock item per type
    List<Stock> exampleStocks = appliancesByType
        .Select(kvp => kvp.Value[0])
        .ToList();
    
    // List of stock items for a given type
    List<Stock> kitchenAppliances = appliancesByType["Kitchen Appliance"];
    

    This approach really takes care of all your needs, as I see it. But for some other options, see below.

    Alternate (quick & dirty) approach

    You can always just use Where to get the items of the type you want, then ToList to put these items in a new List<Stock>:

    List<Stock> kitchenAppliances = stock
        .Where(item => item.Type == "Kitchen Appliance")
        .ToList();
    

    In response to this last part:

    Just another clarification is that I
    also may not provide the parameter
    “Kitchen Appliance” and may just want
    the unique ones, for example It would
    return Kitchen Appliance and Living
    Room once each only to kind of like a
    category no matter how many of that
    Type there are.

    Here, you seem to be wanting something completely different: basically the behavior provided by Distinct. For this functionality, you could essentially go with Soonts’s answer (optionally, returning an IEnumerable<tKey> instead of IEnumerable<tSource>), or you could just leverage Distinct in combination with Select to avoid the need to implement an IEqualityComparer<Stock> (see below).


    Update

    In response to your clarification, here’s my recommendation: two methods, one for each purpose (Single Responsibility Principle):

    // This will return a list of all Stock objects having the specified Type
    static List<Stock> GetItemsForType(string type)
    {
        return stock
            .Where(item => item.Type == type)
            .ToList();
    }
    
    // This will return a list of the names of all Type values (no duplicates)
    static List<string> GetStockTypes()
    {
        return stock
            .Select(item => item.Type)
            .Distinct()
            .ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.