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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:54:38+00:00 2026-05-24T03:54:38+00:00

I have a rather long bit of code I use to count the number

  • 0

I have a rather long bit of code I use to count the number of objects in a list that have one of their properties set to a certain value. However, nearly everything I mentioned above gets passed in at run time, leaving me with this:

((List<MyObject>)this.DataSource).Count(d => d.GetType().GetProperty(MyProperty).GetValue(d, null).ToString() == ValueToMatch);

The problem is that the list is stored as an object in the Datasource property. In order to use count, currently I have to cast this object to a list of type MyObject. I’d like to be able to store any kind of IEnumerable in DataSource. I know that the object stored in Datasource will always be a List, but I’m having trouble convincing C# of this fact. It keeps grumbling about type safety.

I’m in .Net 3.5, so upcasting to a List< Object > fails at run time due to co/contravariance (I can never remember which one is which). I similarly cannot cast to an IEnumerable. Is there any way to make this piece of code work with any kind of 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-24T03:54:39+00:00Added an answer on May 24, 2026 at 3:54 am

    How about this:

    ((IEnumerable)this.DataSource).Cast<object>().Count(d => d.GetType().GetProperty(MyProperty).GetValue(d, null).ToString() == ValueToMatch);
    

    Enumerable.Cast is one of the few extension methods that works on the non-generic IEnumerable. It returns an IEnumerable<T> after which you can use the other extension methods like Enumerable.Count.

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

Sidebar

Related Questions

I have found that writing PHP code within classes can become rather long: $this->parser->parse_syntax($this->get_language_path($this->language),
I have some Javascript code that needs to be dynamically created as a String
I have a feeling this is a rather trivial question, but I'm stumped. In
I'm afraid I have a question on a detail of a rather oversaturated topic,
I would like to run an executable that performs some processing on a dataset
I have been working on JUNG2 for a while now, trying to visualize a
I am busy writing a program in Visual Basic .NET and I have a
I have a solution with multiple projects (a website and various other projects for
I'm currently, in C#, trying to figure out a way of finding a specific
Short Description: I am iterating over a vector calling a virtual function on every

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.