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 a task that takes a rather long time and should run in
I have a long running Task that uses callbacks to feed data incrementally (rather
I have some code that does a lot of comparisons of 64-bit integers, however
I am rather confused I have got a createuser wizard control that I use
I have a rather long switch-case statement. Some of the cases are really short
I have rather large project that uses ICU regex classes. Basically it might run
Have a rather simple question. Does anyone knows if i can use jparallax both
I have a long, lazy sequence that I want to reduce and test lazily.
I have some code that prints the amount of memory used by the program.
I have a bit of a problem. I am currently makeing a webpage that

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.