How to access at run-time the value of an IEnumerable<T>

Jose Daniel Navarro Brito 61 Reputation points
2024-11-21T12:22:44.3566667+00:00

Good day all:

I'm developing a WEB API that returns a paged response to a jQuerydatatable end point. Because the code repeats itself, I encapsulated all the logic in the generic procedure below. The code works perfectly when I pass the class (entity) I want the SQL stored procedure to page. The issue is that I want to access the value of two of the properties return by the IEnumerable <T> namely TotalCount and FilteredCount which are always returned by the stored procedure

public async Task<OutgoingResponseToEndPointDatatable<T>> SendingDataBackToEndPointJqueryDatatable(OncomingEndPointDatatableRequest request, string NameOfStoredProcedure)

{

 var parameters = new StoredProcedureParameters()

 {

     PageNo = Convert.ToInt32(request.start / request.length),

     PageSize = request.length,

     SortColumn = request.Order[0].Column,

     SortDirection = request.Order[0].Dir,

     SearchValue = request.Search != null ? request.Search.Value.Trim() : ""

 };

 IEnumerable<T> outcome= await  _db.LoadPaginationSearchAndSortingStoredProcedure<T, dynamic>(storedProcedure: NameOfStoredProcedure, parameters: parameters);

How I can refer to the int values of the TotalCount and FitredCount properties return by the procedure above? I tried using var properies=trypeof(T). GetProperties but an object is return and I want to access to the int values not the object.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,704 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,152 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
355 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 68,486 Reputation points
    2024-11-21T16:25:15.56+00:00

    implement an interface (or base class) implemented by the sp return classes

    	public interface IQueryResult
    	{
    		int TotalCount {get; set;}
    		int FilteredCount {get; set;}
    	}
    

    then require the interface in the method definition

    public T Test<T>(T p) where T: IQueryResult
    {
    }	
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.