覆盖抽象类的属性

时间:2011-04-09 23:11:40

标签: c# casting override virtual abstract

ODS List是一组抽象类,它们为分页/排序的对象数据源实现过滤的select方法。我已经定义了三个absract类,它们代表过滤器,返回的数据以及产生这些结果的方法:

[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;
    }

}

当我尝试将ListData强制转换为显式返回的数据时,我的实现遇到了问题。

[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
    }

我点击的诊断是:

无法转换类型为'System.Data.Linq.DataQuery 1[BusinessLayer.EmployeeData]' to type 'System.Linq.IQueryable 1 [ODSList.ListData]'的对象。

2 个答案:

答案 0 :(得分:0)

你可以尝试

return allrows.AsQueryable();

我不确定,但你可能需要做

return allrows.Cast<ListData>().AsQueryable(); // if EmployeeData inherits from ListData; it is not required.
                                               // but i'm not sure if ListData is the same as ODSList.ListData

答案 1 :(得分:0)

您使用的.Net版本是什么?如果您使用的是早于4.0的版本,则通用接口中指定的类型是不变的,这意味着您无法从IQueryable转换为IQueryable。

查看MSDN文章here

编辑:经过一些实验并找到this SO post后,我发现以下内容对您有用:

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>();
}