如何从 linq-to-sql 查询实现自定义返回类型的排序?

发布于 2024-08-17 15:11:59 字数 2806 浏览 6 评论 0原文

我正在使用带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-08-24 15:11:59

您可能想查看:SO

You might want to check out: SO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文