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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:09:39+00:00 2026-06-02T02:09:39+00:00

public class Item { public int Id {get; set;} public bool Selected {get; set;}

  • 0
public class Item
{
    public int Id {get; set;}
    public bool Selected {get; set;}
}

List<Item> itemList = new List<Item>(){ /* fill with items */ };

I need to create a list of Items that meet the following criteria. From itemList, I need to group the items by Id and then choose a single item from each group. The chosen item must be one in which Selected == true. If no item in the group is selected, then any item can be chosen, it doesn’t matter, but one must be chosen.

Based on this question:
How to get distinct instance from a list by Lambda or LINQ

I was able to put together the following, which seems to work:

var distinctList = itemList.GroupBy(x => x.Id, 
    (key, group) => group.Any(x => x.Selected) ? 
        group.First(x => x.Selected) : group.First());

Is there a more efficient or simpler way to achieve this? I tried FirstOrDefault() but couldn’t seem to make it do what I needed. My concern with the efficiency in the above code, is the call to Any().

  • 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-02T02:09:41+00:00Added an answer on June 2, 2026 at 2:09 am

    You can indeed use the FirstOrDefault extension method, but use the version that takes a predicate, and combine it with the coalesce operator (??) like so (I’ve used query syntax here to make it easier):

    var distinctList =
        from item in itemList
        group item by item.Id into g
        select g.FirstOrDefault(i => i.Selected) ?? g.First();
    

    Using the predicate in FirstOrDefault, you are picking the first item where the Selected property is true. If there is none, then assuming your type is a reference type (and this is important this to work with FirstOrDefault), it will return null.

    If null is returned, then you’ll just return the first item in the group (through the call to First), since any item can be returned and a group can’t exist without items in it (so the call to First is always guaranteed to succeed).

    Basically, you are applying a selector to the result of the grouping, not while grouping.

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

Sidebar

Related Questions

Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
I have a model public class Foo{ public int Id{get;set;} public string Name {get;
public class Options { public FolderOption FolderOption { set; get; } public Options() {
I have this class. public class Foo { public Guid Id { get; set;
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
I have a model item public class EntryInputModel { ... [Required(ErrorMessage = Description is
Ok, i got a viewmodel as follows: public class PageViewModel { public Item Item
public class UploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { ... // Create
I've got the following code: public IList<IProductViewModel> ChildProducts { get; set; } public IList<IProductViewModel>
I have the following program: public class Hit { readonly Hits _hits; readonly int

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.