问题描述
我有一个带有 CreatedDate 列的表.我想按数据库中最长的行排序,但需要通过从 DateTime.Now 中减去它来确定天数.
我意识到只需通过 CreatedDate 进行排序就可以做到这一点,但我需要做的还不止这些.
我需要根据物品在一段时间内的使用次数来订购物品,实际上是为了获得最受欢迎的物品.
所以我要做的是首先确定天数,然后将其除以每个项目的使用次数.
步骤 1)
orderby (DateTime.Now.Subtract(t.CreatedDate.Value).Days)
第 2 步)
orderby (t.UsedCount/DateTime.Now.Subtract(t.CreatedDate.Value).Days)
这会导致以下错误:
<块引用>方法"System.TimeSpan Subtract(System.DateTime)"不支持 SQL 转换.
推荐答案
奇怪的是,"Subtract()"不起作用,但 Minus 起作用.试试:
orderby (t.UsedCount/(DateTime.Now - t.CreatedDate.Value).Days)
(使用"主"数据库在 LINQPad 中测试)
from sp in Spt_monitors orderby sp.Io_busy / (DateTime.Now - sp.Lastrun).Days select sp
问题描述
I have a table with a CreatedDate column. I want to order by the rows that have been in the database the longest, but need to determine the number of days by subtracting it from DateTime.Now.
I realise this is doable simply by ordering by the CreatedDate, but I need to do more than that.
I need to order the items by the amount of times they have been used within a period of time, in effect getting the most popular items.
So what I want to do is first determine the number of days, and after that divide this by the number of times each item has been used.
Step 1)
orderby (DateTime.Now.Subtract(t.CreatedDate.Value).Days)
Step 2)
orderby (t.UsedCount/DateTime.Now.Subtract(t.CreatedDate.Value).Days)
This results in the following error:
Method 'System.TimeSpan Subtract(System.DateTime)' has no supported translation to SQL.
推荐答案
Oddly, "Subtract()" doesn't work, but Minus does. try:
orderby (t.UsedCount/(DateTime.Now - t.CreatedDate.Value).Days)
(Tested in LINQPad using the "master" database)
from sp in Spt_monitors orderby sp.Io_busy / (DateTime.Now - sp.Lastrun).Days select sp