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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:00:18+00:00 2026-05-14T04:00:18+00:00

I’m trying to run the Count() function of a Linq statement in an overriden

  • 0

I’m trying to run the Count() function of a Linq statement in an overriden Gridview function. Basically, I want to be able to assign a linq query to a gridview, and on the OnDataBound(e) event in my new extended gridview have it retrieve the count, using the IQueryable.

So, I need to dynamically cast the GridView.DataSource to my LinqToSql IQueryable object so that i can execute the Count() function against it.

This is where I’m at so far:

protected override void OnDataBound(EventArgs e)
    {
        IEnumerable _data = null;
        if (this.DataSource is IQueryable)
        {
            _data = (IQueryable)this.DataSource;
        }
        System.Type dataSourceType = _data.GetType();
        System.Type dataItemType = typeof(object);
        if (dataSourceType.HasElementType)
        {
            dataItemType = dataSourceType.GetElementType();
        }
        else if (dataSourceType.IsGenericType)
        {
            dataItemType = dataSourceType.GetGenericArguments()[0];
        }
        else if (_data is IEnumerable)
        {
            IEnumerator dataEnumerator = _data.GetEnumerator();

            if (dataEnumerator.MoveNext() && dataEnumerator.Current != null)
            {
                dataItemType = dataEnumerator.Current.GetType();
            }
        }
        Object o = Activator.CreateInstance(dataItemType);
        object[] objArray = new object[] { o };
        RowCount = (int)dataSourceType.GetMethod("Count").Invoke(_data, objArray);

Any ideas? I’m really new with working with IQueryables and Linq so I may be way off. How can I get my _data to allow me to run the Count function?

  • 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-14T04:00:19+00:00Added an answer on May 14, 2026 at 4:00 am

    This is probably the easiest way (and is somewhat related to your own answer):

    protected override void OnDataBound(EventArgs e) {
        var count = 0;
        if (DataSource is IQueryable) {
            count = ((IQueryable)DataSource).OfType<object>().Count();
        }
        else {
            count = ((IEnumerable)DataSource).OfType<object>().Count();
        }
    
        // use count here
    }
    

    Assuming that the query provider is intelligent enough to ignore the conversion to object, this should result in a DB query to get the count if the source is actually an IQueryable.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.