订购的LINQ到实体多列 [英] Ordering in Linq to Entities Multiple Columns

查看:134
本文介绍了订购的LINQ到实体多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server中的表有以下几列




  • ID(身份/主键)

  • DestinationID(外键)

  • 日期(日期字段)

  • 时间(场)

  • DAYOFWEEK(CHAR(3)现场),即MON,TUE等。



我要订购数据按日期和时间,然后通过星期几和时间。因此,所有的日期记录出现在顶部,并且按时间按时间,那么DAYOFWEEK顺序排序。



我尝试了好几种方法,但只是不能左右我的头它,我有以下代码

  VAR QRY = ctx.DestinationTimings.Where(X => x.DestinationID ==这.ID)
.OrderBy(T => t.Date).ThenBy(T =>!t.Date = NULL)
.ThenBy(T => SqlFunctions.CharIndex(t.DayOfWeek +,,星期一,星期二,星期三,星期四,星期五,星期六,星期日))
.ThenBy(T => t.Time);


解决方案

如果日期可为空,你可以做这样的事情:

  VAR QRY = ctx.DestinationTimings.Where(X => x.DestinationID == this.ID)
.OrderBy(T => t.Date ?? DateTime.MaxValue)
.ThenBy(T => SqlFunctions.CharIndex(t.DayOfWeek +, ,星期一,星期二,星期三,星期四,星期五,星期六,星期日))
.ThenBy(T => t.Time);


I have a table in SQL Server that has the following columns

  • ID (Identity/Primary Key)
  • DestinationID (Foreign key)
  • Date (Date Field)
  • Time (Time Field)
  • DayOfWeek (Char(3) Field) i.e. "MON","TUE" etc..

I want to order the data by Date and Time, then by DayOfWeek and Time. So all the records with dates appear at the top, and ordered by time, then the DayOfWeek order by time.

I have tried several ways but just cant get my head around it, I have the following code

    var qry = ctx.DestinationTimings.Where(x => x.DestinationID == this.ID)
                     .OrderBy(t => t.Date).ThenBy(t => t.Date != null)
                     .ThenBy(t => SqlFunctions.CharIndex(t.DayOfWeek + ",", "MON,TUE,WED,THU,FRI,SAT,SUN"))
                     .ThenBy(t => t.Time);

解决方案

If Date is nullable you could do something like this:

var qry = ctx.DestinationTimings.Where(x => x.DestinationID == this.ID)
                 .OrderBy(t => t.Date ?? DateTime.MaxValue)
                 .ThenBy(t => SqlFunctions.CharIndex(t.DayOfWeek + ",", "MON,TUE,WED,THU,FRI,SAT,SUN"))
                 .ThenBy(t => t.Time);

这篇关于订购的LINQ到实体多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆