根据this site,黑客新闻的排序算法是这样的:(p – 1) / (t + 2)^1.5Description:Votes divided by age factorp = votes (points) from users. t =time since submission in hours.p is subtracted by 1 to nega...
根据this site,黑客新闻的排序算法是这样的:
(p – 1) / (t + 2)^1.5
Description:
Votes divided by age factor
p = votes (points) from users. t =
time since submission in hours.p is subtracted by 1 to negate
submitters vote. age factor is (time
since submission in hours plus two) to
the power of 1.5.
给定一个与此类似的表结构:
Item
ID
Link
DatePostedItem_Votes
ItemID
Value
使用linq to sql实现算法的最佳方法是什么,我能够完全在linq中编写查询,还是需要使用存储过程或其他东西.
更新.根据TJB的答案结束使用下面的代码:
var votesQuery =
from i in db.Items
join v in db.Item_Votes on i.ItemID equals v.ItemID
orderby
(double)(v.Value - 1) /
Math.Pow(
(DateTime.Now - i.DatePosted.Value).TotalHours + 2,
1.5) descending
select i;
解决方法:
使用2 1 Linq查询(可能还有更有效的方法)
var votesQuery =
from i in items
join v in votes on i.Id equals v.ItemId
orderby
(v.Value - 1) /
Math.Pow(
(DateTime.Now - i.Posted).Add(new TimeSpan(2,0,0)).Hours,
1.5 )
select new
{
Item = i,
Vote = v
};
本文标题为:c# – Linq-To-SQL中的黑客新闻样式排序算法
- C#中结构体定义并转换字节数组详解 2022-11-19
- WPF使用WrapPanel实现虚拟化效果 2023-07-04
- C#实现循环发送电脑屏幕截图 2023-04-27
- C#面向对象设计原则之开闭原则 2023-05-25
- C#多线程系列之多阶段并行线程 2023-05-22
- 浅谈c# 浮点数计算 2022-11-15
- C#多线程之线程同步 2023-05-26
- C#实现OFD格式与PDF格式的互转 2023-05-17
- C#获取计算机硬件与操作系统的相关信息 2023-06-05
- .net core Docker Compose启动问题: Debugging Error, The program to be debug con not be found in the conta 2023-09-28