How to return generic data from method?

//Get all event details
public IEnumerable GetAllData()
{
return (from t in _sqlconnection.Table() select t).ToList();
}

I have method that return the all rows from database. but i want to make it generic. Like whatever table i pass in method that returns the all the rows from table.
How can i do it?

Answers

  • MabroukMabrouk USMember ✭✭✭
    edited July 2017

    Hi @Jaymin

    I think you are looking for something like :

    public R GetData<T,R>() where T:class // class or your base object of tables..
            {
                string tableName = typeof(T).Name;
                string query = $"select * from {tableName}";
                // Excute your qury
                // collect result data
                R result=.........................................;
                return result;
            }
    

    In general, you need to use Generic Types :smile:

    I hope that helps,
    Mabrouk.

Sign In or Register to comment.