如何从 linq-to-sql 查询实现自定义返回类型的排序?
我正在使用带有 linq-to-sql 的存储库类作为(Web)GridView 的对象数据源。 GridView 必须允许对所有列进行排序。我有一个使用 这种方法,但我显然更愿意在没有预定义的排序表达式列表的情况下执行此操作。
public class TrailerMovementRepository
{
private TrailerMovementDataContext db = new TrailerMovementDataContext();
public IOrderedEnumerable<TrailerMovementHistory> GetTrailerMovementHistoryByDepotAndDate(string depot, DateTime searchDate, string sortExpression)
{
var unorderedQuery = (from tm in db.TrailerMovements
where tm.Depot == depot && tm.Date_In == searchDate && tm.Time_Out != null
select new TrailerMovementHistory
{
Depot = tm.Depot,
TrailerNumber = tm.Trailer,
TimeIn = tm.Time_In,
TimeOut = tm.Time_Out,
VOR = tm.VOR.Value,
Contents = tm.Contents,
Supplier = tm.Supplier,
TurnaroundTime = FormatDuration(tm.Time_Out - tm.Time_In),
VORTime = FormatDuration(tm.VOnR_Date - tm.VOffR_Date),
LoadedTime = tm.LoadedTime,
Destination = tm.Destination
}).ToList<TrailerMovementHistory>();
//need to find a way to dynamically do this from the passed in expression
IOrderedEnumerable<TrailerMovementHistory> orderedQuery = unorderedQuery.OrderBy(t => t.TrailerNumber);
switch (sortExpression)
{
case "TrailerNumber DESC":
orderedQuery = unorderedQuery.OrderByDescending(t => t.TrailerNumber);
break;
case "TimeIn":
orderedQuery = unorderedQuery.OrderBy(t => t.TimeIn);
break;
case "TimeIn DESC":
orderedQuery = unorderedQuery.OrderByDescending(t => t.TimeIn);
break;
...etc...
default:
break;
}
return orderedQuery;
}
public class TrailerMovementHistory
{
public TrailerMovementHistory()
{ }
public String Depot { get; set; }
public String TrailerNumber { get; set; }
public DateTime? TimeIn { get; set; }
public DateTime? TimeOut { get; set; }
public Boolean VOR { get; set; }
public String Contents { get; set; }
public String Supplier { get; set; }
public String TurnaroundTime { get; set; }
public String VORTime { get; set; }
public DateTime? LoadedTime { get; set; }
public String Destination { get; set; }
}
}
I am using a repository class with linq-to-sql as the objectdatasource for a (web) GridView. The GridView has to allow sorting on all columns. I have a working solution using this approach but I would obviously prefer to do this without a predefined list of sort expressions.
public class TrailerMovementRepository
{
private TrailerMovementDataContext db = new TrailerMovementDataContext();
public IOrderedEnumerable<TrailerMovementHistory> GetTrailerMovementHistoryByDepotAndDate(string depot, DateTime searchDate, string sortExpression)
{
var unorderedQuery = (from tm in db.TrailerMovements
where tm.Depot == depot && tm.Date_In == searchDate && tm.Time_Out != null
select new TrailerMovementHistory
{
Depot = tm.Depot,
TrailerNumber = tm.Trailer,
TimeIn = tm.Time_In,
TimeOut = tm.Time_Out,
VOR = tm.VOR.Value,
Contents = tm.Contents,
Supplier = tm.Supplier,
TurnaroundTime = FormatDuration(tm.Time_Out - tm.Time_In),
VORTime = FormatDuration(tm.VOnR_Date - tm.VOffR_Date),
LoadedTime = tm.LoadedTime,
Destination = tm.Destination
}).ToList<TrailerMovementHistory>();
//need to find a way to dynamically do this from the passed in expression
IOrderedEnumerable<TrailerMovementHistory> orderedQuery = unorderedQuery.OrderBy(t => t.TrailerNumber);
switch (sortExpression)
{
case "TrailerNumber DESC":
orderedQuery = unorderedQuery.OrderByDescending(t => t.TrailerNumber);
break;
case "TimeIn":
orderedQuery = unorderedQuery.OrderBy(t => t.TimeIn);
break;
case "TimeIn DESC":
orderedQuery = unorderedQuery.OrderByDescending(t => t.TimeIn);
break;
...etc...
default:
break;
}
return orderedQuery;
}
public class TrailerMovementHistory
{
public TrailerMovementHistory()
{ }
public String Depot { get; set; }
public String TrailerNumber { get; set; }
public DateTime? TimeIn { get; set; }
public DateTime? TimeOut { get; set; }
public Boolean VOR { get; set; }
public String Contents { get; set; }
public String Supplier { get; set; }
public String TurnaroundTime { get; set; }
public String VORTime { get; set; }
public DateTime? LoadedTime { get; set; }
public String Destination { get; set; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想查看:SO
You might want to check out: SO