can I use a dynamic entity context in my method?

asp.net c# dynamic-linq entity-framework-5 generics

Question

Can somebody pls assist me? I have a technique for sorting and panning gridviews that I'm attempting to make reusable for generic types, but I'm having trouble making the entity context (MyContext) and table (tblReports) dynamic variables.

protected void PopulateGridView<T>(GridView grid)
{
    string columnToSortBy = (string)(ViewState["gridview_sortbycolumn"] ?? string.Empty);
    SortDirection sortDirection = (SortDirection)(ViewState["gridview_sortdirection"] ?? SortDirection.Descending);
    int pageIndex = (int)(ViewState["gridview_pageindex"] ?? 0);
    int pageSize = grid.PageCount;

    using (MyContext db = new MyContext())
    {
        if (columnToSortBy != string.Empty)
        {
            var query = db.tblReports.OrderBy(columnToSortBy + " " + sortDirection).Skip(pageIndex * pageSize);
            grid.DataSource = query.ToList();
        }
        else
        {
            string defaultColumn = string.Empty;

            foreach (var field in typeof(T).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                defaultColumn = field.Name.Remove(field.Name.IndexOf(">")).Replace("<", string.Empty);
                break;
            }

            var query = db.tblReports.OrderBy(defaultColumn).Skip(pageIndex * pageSize);
            grid.DataSource = query.ToList();
        }

        grid.PageIndex = pageIndex;
        grid.DataBind();
    }
}
1
1
9/5/2013 10:35:34 AM

Accepted Answer

I was able to get it to work by turning the results of the linq query to a generic type, as @Jeroen advised, then supplying the entity table as a parameter.

For anybody interested, here is the whole code:

protected void PopulateGridView<T>(GridView grid, DbSet entityTable)
{
    string columnToSortBy = (string)(ViewState["gridview_sortbycolumn"] ?? string.Empty);
    SortDirection sortDirection = (SortDirection)(ViewState["gridview_sortdirection"] ?? SortDirection.Descending);
    int pageIndex = (int)(ViewState["gridview_pageindex"] ?? 0);

    if (columnToSortBy != string.Empty)
    {
        grid.DataSource = entityTable.OrderBy(columnToSortBy + " " + sortDirection).Cast<T>().ToList();
    }
    else
    {
        // use linq reflection to get the first entity field name to sort by
        string defaultColumn = string.Empty;

        foreach (var field in typeof(T).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
        {
            defaultColumn = field.Name.Remove(field.Name.IndexOf(">")).Replace("<", string.Empty);
            break;
        }

        grid.DataSource = entityTable.OrderBy(defaultColumn).Cast<T>().ToList();
    }

    grid.PageIndex = pageIndex;
    grid.DataBind();
}
0
9/5/2013 1:05:36 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow