[英]GridView Filtering with ObjectDataSource using Custom Paging
[英]GridView ObjectDataSource LINQ Paging and Sorting using multiple table query
我正在尝试创建一个分页和排序对象数据源,该数据源在执行之前返回所有结果,然后在过滤之前对这些结果进行排序,然后使用take和skip方法,目的是仅从数据库中检索结果的一部分(保存在数据库流量)。 这是基于以下文章:
http://www.singingeels.com/Blogs/Nullable/2008/03/26/Dynamic_LINQ_OrderBy_using_String_Names.aspx
现在,即使创建了lambda表达式来反映从网格返回的排序表达式,甚至找到要对DateTime和Decimal进行排序的数据类型,我都设法使它起作用。
public static string GetReturnType<TInput>(string value)
{
var param = Expression.Parameter(typeof(TInput), "o");
Expression a = Expression.Property(param, "DisplayPriceType");
Expression b = Expression.Property(a, "Name");
Expression converted = Expression.Convert(Expression.Property(param, value), typeof(object));
Expression<Func<TInput, object>> mySortExpression = Expression.Lambda<Func<TInput, object>>(converted, param);
UnaryExpression member = (UnaryExpression)mySortExpression.Body;
return member.Operand.Type.FullName;
}
现在我遇到的问题是,许多查询返回联接的表,我想对其他表中的字段进行排序。
因此,在执行查询时,您可以创建一个函数,该函数会将其他表中的属性分配给在部分类中创建的属性。
public static Account InitAccount(Account account)
{
account.CurrencyName = account.Currency.Name;
account.PriceTypeName = account.DisplayPriceType.Name;
return account;
}
所以我的问题是,有没有办法将联接表中的值分配给当前表子类的属性? 我尝试使用。
from a in dc.Accounts
where a.CompanyID == companyID
&& a.Archived == null
select new {
PriceTypeName = a.DisplayPriceType.Name})
但这似乎弄乱了我的SortExpression。
任何帮助,我们将不胜感激,我知道这是很复杂的东西。
这是功能性编程。 通过执行任务来更改帐户已退出。 新建所需形状的新实例。
步骤1:声明一个具有所需结果形状的类:
public class QueryResult
{
public int CompanyID {get;set;}
public string CurrencyName {get;set;}
public string PriceTypeName {get;set;}
}
步骤2:在查询中投影到该类中
from ...
where ...
select new QueryResult()
{
CompanyID = a.CompanyID,
CurrencyName = a.Currency.Name,
PriceTypeName = a.PriceType.Name
};
第三步:赢利! (按此排序)
查询生成器将使用QueryResult类型的详细信息来生成具有该形状的select子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.