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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:13:01+00:00 2026-05-25T20:13:01+00:00

i have an entity called Product class Product { public Id { get; set;

  • 0

i have an entity called Product

class Product
{
     public Id { get; set; }
     public Name { get; set; }
}

and i have a list of all products:

IEnumerable<Product> products = _productRepository.GetAll()

i want to get an array of strings from this list of products this array will contains the product Id + Product Name, so when i try to cast it using the following code:

string[] s = products.Cast<string>().ToArray();

i got the following exception:

Unable to cast object of type 'Product' to type 'System.String'

the exception really makes alot fo scence, so if i had a method

string ProductToString(Product p)
{
    return p.Name;
}

or an override to ToString() for the product object so how i can use this method to get the list of string[] from IEnumerable ?

  • 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-25T20:13:02+00:00Added an answer on May 25, 2026 at 8:13 pm

    Well, given that method you can use1:

    string[] s = products.Select<string>(ProductToString).ToArray();
    

    However, it would be more idiomatic to do this without a separate method, usually, using a lambda expression:

    // Matches ProductToString, but not your description
    string[] s = products.Select(p => p.Name).ToArray();
    

    I’d only use a separate method if it was going to be called from various places (ensuring consistency) or did a lot of work.

    EDIT: I’ve just noticed that your description (wanting ID + name) doesn’t actually match the ProductToString method you’ve given (which just gives the name). For the ID + name I’d use:

    string[] s = products.Select(p => p.ID + " " + p.Name).ToArray();
    

    or

    string[] s = products.Select(p => string.Format("{0} {1}", p.ID, p.Name))
                         .ToArray();
    

    Or you could just change your ProductToString method, of course.

    Alternatively, you could override ToString() in Product, if this is usually how you want to convert a Product to a string. You could then either use a method group conversion or a lambda expression to call ToString.


    1 It’s possible that you don’t need to specify the type argument explicitly – that:

    string[] s = products.Select(ProductToString).ToArray();
    

    will work fine – the rules for type inference and method group conversions always confuse me and the compiler behaviour has changed slightly over time. A quick test just now looks like it does work, but there could be subtleties in slightly different situations.

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

Sidebar

Related Questions

Imagine I have an entity called Product and a repository for it: public class
I have this Entity called 'Operation': @Entity @Table(name=operation) public class Operation implements Serializable {
I have a entity called Product,this is a part of it's declration: [EdmEntityTypeAttribute(NamespaceName=NorthwindModel, Name=Product)]
I have entity called Customer and it has three properties: public class Customer {
I have a project in Asp net mvc. An entity called product can not
We have an advanced product option created called Courses. It contains all the fields
I have an entity called Medico which was created as an Entity Class from
I have an entity called Category and the entity contains a IEnumerable called ChildCategories.
I have an entity called Account and I have given this entity a class
I have an abstract entity called Block which contains two attributes: column and order

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.