本文介绍了在LINQ到SQL黑客新闻的风格排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据对于黑客新闻的排序算法是这样的:



Given a table structure similar to this:

What would be the best way to implement the algorithm using linq to sql, would I be able to write the query entirely in linq or would I need to use a stored procedure or something else.

Update. Ended up using the code below based off TJB's answer:

    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;
解决方案

Using 1 Linq querie (there's probably still a more efficent way)

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
    };

这篇关于在LINQ到SQL黑客新闻的风格排序算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:42