La liste ODS est une collection de classes abstraites qui implémentent une méthode select filtrée pour une source de données d'objet paginé/trié. J'ai défini trois classes absract qui représentent le filtre, les données renvoyées et les méthodes qui produisent ces résultats:Propriétés de substitution de la classe abstraite
[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;
}
}
Ma mise en œuvre frappe un problème lorsque je tente de lancer le ListData aux données explicites de retour.
[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
}
Le diagnostic je frappe est:
Impossible de coulée objet de type 'System.Data.Linq.DataQuery 1[BusinessLayer.EmployeeData]' to type 'System.Linq.IQueryable
1 [ODSList.ListData].
Cela pourrait bien être la solution. Je suis sur 4.0 @ work et 3.5 @ home (où j'écris ceci). Je vais tester lundi. –
désolé, il m'a fallu si longtemps pour le confirmer, mais votre solution a fonctionné. –