根据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-12-30
- Unity UGUI控制text文字间距 2023-01-16
- C#结合AForge实现摄像头录像 2022-11-18
- C#如何使用Bogus创建模拟数据示例代码 2023-01-22
- C# TSC打印二维码和条形码的实现方法 2022-12-05
- C#中RSA加密与解密的实例详解 2023-01-27
- C#内置泛型委托之Func委托 2023-05-25
- C#实现Base64编码与解码及规则 2023-04-27
- C#使用yield关键字构建迭代器详解 2022-11-18
- C#使用第三方组件实现动态解析和求值字符串表达式 2023-06-20