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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:34:27+00:00 2026-06-07T12:34:27+00:00

I have a generic list, which I use linq to search like so: NotificationWindowItem

  • 0

I have a generic list, which I use linq to search like so:

NotificationWindowItem item = itemList.Where(elm => elm.UID == UID).SingleOrDefault();
itemList.Where(elm => elm.UID == UID).SingleOrDefault().Read = true;

I then go on to call a WCF web service with values from item.
I do this since it is my understanding that SingleOrDefault() returns a new IEnumerable, with copies of the objects. (Did I misunderstand?)
The list is usually not very long, so the extra iteration is not very significant, but it really bugs me.

I just can’t think of a way to consolidate the two iterations.
Any ideas?

  • 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-07T12:34:28+00:00Added an answer on June 7, 2026 at 12:34 pm

    SingleOrDefault does not return an IEnumerable but a single item instead. Since you already assign and keep it in your variable item, why not simply use that for further processing?

    NotificationWindowItem item = itemList.Where(elm => elm.UID == UID).SingleOrDefault();
    item.Read = true;
    

    You can further simplify this. And, as pointed out by Tim, you need a null check (see below why):

    NotificationWindowItem item = itemList.SingleOrDefault(elm => elm.UID == UID);
    if (item == null)
    {
        // ... some alternative or error handling code
    }
    else
    {
        item.Read = true;
    }
    

    To expand on your questions/unclarities:

    1. itemList is a list of item. Each item is a NotificationWindowItem. The list implements the IEnumerable interface. (More precisely, I am assuming that itemList is a List<NotificationWindowItem> which implements IEnumerable<NotificationWindowItem>.
    2. Where is an extension method that takes the IEnumerable<NotificationWindowItem> and creates another IEnumerable<NotificationWindowItem> that only contains matching elements.
    3. SingleOrDefault takes an IEnumerable<NotificationWindowItem> and returns a normal, simple, single NotificationWindowItem. (More than that, it verifies that there only is a single matching element. If more than one element match it would throw an exception. If no element matches, it returns default(T) which is null in your case).
    4. item is not an IEnumerable. It is a NotificationWindowItem. There is no magic behind. It is a simple object. It does not have anyhting to do with LINQ. You only used LINQ to retrieve it, but afterwards you can do anything you could do with any other instance of NotificationWindowItem.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
I have generic list which must be a preserved order so I can retrieve
I have a Generic List object which hold below table as its own value.
Can anyone help.. I have a generic list like so IList<itemTp> itemTps; itemTp basically
I have the following code which builds a Generic List of Abc from a
I have a question about generic list. Assume that I have a database which
I have two classes, CheckboxItemsList which extends a generic list, and CheckboxItems , which
I have a generic list which can contain values in different languages based on
I have a generic List<MyClass> where MyClass has a property InvoiceNumber which contains values
I have the following Generic List which is populated with a list of string:

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.