[英]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. ODS List是抽象类的集合,这些抽象类为分页/排序的对象数据源实现过滤的选择方法。 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. 当我尝试将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
}
The diagnostic I hit is: 我遇到的诊断是:
Unable to cast object of type 'System.Data.Linq.DataQuery 1[BusinessLayer.EmployeeData]' to type 'System.Linq.IQueryable
1[ODSList.ListData]'. 无法将类型为“ System.Data.Linq.DataQuery
1[BusinessLayer.EmployeeData]' to type 'System.Linq.IQueryable
对象强制转换1[BusinessLayer.EmployeeData]' to type 'System.Linq.IQueryable
1 [ODSList.ListData]”的对象。
You can try 你可以试试
return allrows.AsQueryable();
and I'm not sure but you may need to do 我不确定,但您可能需要做
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
What version of .Net are you using? 您正在使用哪个版本的.Net? 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.
如果您使用的版本早于4.0,则通用接口中指定的类型是不变的,这意味着您无法从IQueryable转换为IQueryable。
Check out the MSDN article here . 在此处查看MSDN文章。
edit: After a bit of experimenting and finding this SO post , I found that something like the following should work for you: 编辑:经过一些试验并找到了此SO帖子 ,我发现类似以下内容的内容应该对您有用:
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>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.