Search code examples
c#castingoverridingvirtualabstract

Overriding properties of abstract class


ODS List is a collection of abstract classes that implement a filtered select method for a paged/sorted object datasource. I have defined three absract classes that represent the filter, the returned data and the methods that produce these results:

[Serializable]
public abstract class ListFilter
{
    //  Define filter properties
}

[Serializable]
public abstract class ListData
{
    //  Define properties to be returned from the query
}
public abstract class ListMethods
{
    public int ListCount(ListFilter filter)
    {
        var rows = listQuery();
        rows = listFilter(rows, filter);
        return rows.Count();
    }

    /// <summary>
    /// List returns a page of data 
    /// </summary>
    /// <param name="filter"></param>
    /// <param name="sortType"></param>
    /// <param name="startRowIndex"></param>
    /// <param name="maximumRows"></param>
    /// <returns></returns>
    public IEnumerable<ListData> List(ListFilter filter, string sortType, int startRowIndex, int maximumRows)
    {
        var rows = listQuery();
        rows = listFilter(rows, filter);
        rows = listSort(rows, sortType);
        return rows.Distinct().Skip(startRowIndex).Take(maximumRows).ToList();
    }

    public abstract IQueryable<ListData> listQuery();

    public virtual IQueryable<ListData> listFilter(IQueryable<ListData> rows, ListFilter filter)
    {
        return rows;
    }

    public virtual IQueryable<ListData> listSort(IQueryable<ListData> rows, string sortType)
    {
        bool sortDescending = false;
        if (!string.IsNullOrEmpty(sortType))
        {
            string[] values = sortType.Split(' ');
            sortType = values[0];
            if (values.Length > 1)
            {
                sortDescending = values[1] == "DESC";
            }
        }


        if (!string.IsNullOrEmpty(sortType))
        {
            if (sortDescending)
            {
                rows = rows.OrderBy(sortType + " DESC");
            }
            else
            {
                rows = rows.OrderBy(sortType);
            }
        }

        return rows;
    }

}

My implementation hits a problem when I try to cast the ListData to the explicit returned data.

[Serializable]
public class EmployeeData : ODSList.ListData
{
    public int EmployeeId { get; set; }
    public int? ReportsToId { get; set; }...
}

    public override IQueryable<ListData> listQuery()
    {
        var dc = new NorthwindDataContext();
        var allrows = from emp in dc.Employees
                    select new EmployeeData()
                    {
                        EmployeeId = emp.EmployeeID,
                        ReportsToId = emp.ReportsTo, ...
                    };
        return (IQueryable<ListData>)allrows; <-- PROBLEM ENCOUNTERED HERE
    }

The diagnostic I hit is:

Unable to cast object of type 'System.Data.Linq.DataQuery1[BusinessLayer.EmployeeData]' to type 'System.Linq.IQueryable1[ODSList.ListData]'.


Solution

  • What version of .Net are you using? If you're on a version earlier than 4.0 the types specified in a generic interface are invariant, meaning you can't cast from IQueryable to IQueryable.

    Check out the MSDN article here.

    edit: After a bit of experimenting and finding this SO post, I found that something like the following should work for you:

    public override IQueryable<ListData> listQuery()
    {
        var dc = new NorthwindDataContext();
        var allrows = from emp in dc.Employees
                    select new EmployeeData()
                    {
                        EmployeeId = emp.EmployeeID,
                        ReportsToId = emp.ReportsTo, ...
                    } as ListData;
        return allrows.OfType<ListData>();
    }