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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:12:29+00:00 2026-05-28T03:12:29+00:00

I am not using Single in LINQ below, but I am still getting a

  • 0

I am not using Single in LINQ below, but I am still getting a ‘Sequence contains no elements’ exception:

allNames = StockCollection.Where((s) => s.Name.IndexOf("A") == 0)
                          .Select((s) => s.Name)
                          .Aggregate((namesInfo, name) => namesInfo += ", " + name);

This exception comes when there is no stock starting with name 'A'.

It seems that one extension method is expecting atleast one element satisfying the condition but that’s not expected.

Can you please suggest the best solution to resolve this?

Thanks in advance.

  • 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-28T03:12:29+00:00Added an answer on May 28, 2026 at 3:12 am

    As Dennis Traub has pointed out, the overload of Aggregate you are using throws that exception when the source sequence is empty.

    The obvious fix is to use the other overload of Aggregate that accepts an initial seed (you want string.Empty), but that would result in a leading comma in the result which you’ll have to get rid of.

    (EDIT: You can dodge this with .DefaultIfEmpty(string.Empty) followed by your existing Aggregate overload. This wouldn’t produce a leading comma.)

    In any case, using Aggregate like that to join strings isn’t a great idea (produces a Schlemiel the Painter’s algorithm). Here’s how I would write the query:

    allNames = string.Join(",", StockCollection.Select(s => s.Name)
                                               .Where(name => name.StartsWith("A"));
    

    In .NET 3.5, you’ll need a .ToArray() to materialize the Where results into an array.

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

Sidebar

Related Questions

Why I am getting this error: The method 'Single' is not supported by LINQ
I am using LINQ to SQL with single table inheritance for an audit log.
I wrote this simple linq-to-xml query and it seems that null exception could not
I am using LINQ to SQL to call a stored procedure with a single
I am using Single data context per atomic operation approach while using Linq to
I'm not sure if this is possible in LINQ, but I have the following
I'm struggling with an exception using linq-to-sql Concat() I've got 2 tables. The first
Using Linq I would like to return an object that contains customers and invoices
I have been using LINQ to SQL for years now, but this is the
Using Linq, trying to select all records not in my local set fails: var

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.