0

I using EntityFramewrok and I want to bind a class with stored procedure.

My procedure select query :

Select RecordId,Name_TrTr Name from Institute where Name_TrTr  like ('%'+@Search+'%')
 Order by Name
 OFFSET @Start ROWS
 FETCH NEXT @Length ROWS ONLY

Select @TotalCount as TotalCount, @FilteredCount FilteredCount

My Classses :

public class InstituteTestModel
{
    public int RecordId { get; set; }
    public string Name { get; set; }
}

public class ProcedureDetail
{
    public int TotalCount { get; set; }
    public int FilteredCount { get; set; }
}

public class ProcedureClass
{
    public InstituteTestModel Model { get; set; }
    public ProcedureDetail Detail { get; set; }
}

C# execute:

var q = Connection.Database<ProcedureClass>(query);

1 Answer 1

1

You can put the result in a datatable and convert your datatable to list

public static class Helper
{
    /// <summary>
    /// Converts a DataTable to a list with generic objects
    /// </summary>
    /// <typeparam name="T">Generic object</typeparam>
    /// <param name="table">DataTable</param>
    /// <returns>List with generic objects</returns>
    public static List<T> DataTableToList<T>(this DataTable table) where T : class, new()
    {
        try
        {
            List<T> list = new List<T>();

            foreach (var row in table.AsEnumerable())
            {
                T obj = new T();

                foreach (var prop in obj.GetType().GetProperties())
                {
                    try
                    {
                        PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);
                        propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
                    }
                    catch
                    {
                        continue;
                    }
                }

                list.Add(obj);
            }

            return list;
        }
        catch
        {
            return null;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.