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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:59:32+00:00 2026-05-11T12:59:32+00:00

Why do I get the following error in the following code? I thought if

  • 0

Why do I get the following error in the following code?

I thought if I put custom objects in a generic List of its type then IEnumerable would be taken care of? What else do I need to do to this List to use LINQ on it?

Cannot implicitly convert type ‘System.Collections.Generic.IEnumerable<TestLinq23.Customer>‘ to ‘TestLinq23.Customer’

using System; using System.Collections.Generic; using System.Linq;  namespace TestLinq23 {     class Program     {         static void Main(string[] args)         {             List<Customer> customerSet = new List<Customer>();             customerSet.Add(new Customer { ID = 1, FirstName = 'Jim', LastName = 'Smith' });             customerSet.Add(new Customer { ID = 2, FirstName = 'Joe', LastName = 'Douglas' });             customerSet.Add(new Customer { ID = 3, FirstName = 'Jane', LastName = 'Anders' });              Customer customerWithIndex = customerSet[1];             Console.WriteLine('Customer last name gotten with index: {0}', customerWithIndex.LastName);              Customer customerWithLinq = from c in customerSet                            where c.FirstName == 'Joe'                            select c;             Console.WriteLine(customerWithLinq.LastName);              Console.ReadLine();         }     }      public class Customer     {         public int ID { get; set; }         public string FirstName { get; set; }         public string LastName { get; set; }     }  } 
  • 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. 2026-05-11T12:59:32+00:00Added an answer on May 11, 2026 at 12:59 pm

    You need to add a call to Single() – otherwise it’s returning a sequence of customers.

    At the same time, there’s no real need to use a query expression here. It’ll be simpler to use dot notation:

    Customer customerWithLinq = customerSet.Where(c => c.FirstName == 'Joe')                                        .Single(); 

    In fact, you can make it even simpler, because there’s an overload of Single() to take a predicate:

    Customer customerWithLinq = customerSet.Single(c => c.FirstName == 'Joe') 

    Is it an error condition if there isn’t exactly one match? If not, you might want to use First() instead of Single().

    EDIT: As pointed out by Garry, if there may be no results you might want SingleOrDefault() or FirstOrDefault() – both of these will return null if no entries match.

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

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • 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 The best I could come up with: >>> import os… May 11, 2026 at 5:51 pm
  • Editorial Team
    Editorial Team added an answer crontabs are files read by the cron daemon. They tell… May 11, 2026 at 5:51 pm
  • Editorial Team
    Editorial Team added an answer First, you need a query to assign a number from… May 11, 2026 at 5:51 pm

Related Questions

I have the following program: ~/test> cat test.cc int main() { int i =
Here's my problem. I have a bunch of figures already written in a LaTeX
I have a dictionary that goes like this: typedef struct dictNode { int key;
I'm writing a model that handles user input from a text area. Following the

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.