LINQ:多个字段排序依据总和 [英] Linq: OrderBy Sum of multiple fields

查看:309
本文介绍了LINQ:多个字段排序依据总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个LINQ EX pression因此我可以指望的事件评论的数量以及SUM的票数。这是我所得到的,但我没有得到相应的结果。

我的例子是在VB.Net做的,但我可以用C#示例以及处理。

 公共功能GetHotEvents(BYVAL跳过为整数)作为目录(中Domain.Event)实现IEventService.GetHotEvents
        ''#我们的订单通过序列只是需要票,乘人数
        ''#这两个。然后采取的评论的数量,并将其添加到
        ''#计票。这样,向上投事件将登顶,而
        ''#下投票活动将落到底部。评论也增加了
        ''#事件的辣味。
        返回_EventRepository.Read()_
            。凡(功能(五)e.EventDate> = Date.Today)_
            .OrderBy(功能(E)(((e.EventVotes.Sum(功能(S)s.Vote))* 2)+(e.Comments.Count)))_
            .Skip(跳过)_
            。取(5)_
            .ToList()
    端功能
 

我已经做了什么来测试,这是对所有事件ZERO意见,并upvote一个事件。这一事件应该浮到顶部,但它不存在。

任何帮助将是很大的AP preciated。

修改

我已经试过建设EX pression在LinqPad,但遗憾的是它抛出一个错误(注:此错误不会让我的code抛出)

  

重载决策失败,因为没有   访问排序依据可以被称为   这些论点:       扩展方法公共功能的OrderBy(TKEY的中)(的KeySelectors作为   System.Linq.Ex pressions.Ex pression(中   System.Func(LINQPad.User.Events中,   TKEY的)))作为   System.Linq.IOrderedQueryable(中   LINQPad.User.Events)的'定义   System.Linq.Queryable:评论是   不是LINQPad.User.Events的成员。       扩展方法公共功能的OrderBy(TKEY的中)(的KeySelectors作为   System.Linq.Ex pressions.Ex pression(中   System.Func(LINQPad.User.Events中,   TKEY的)))作为   System.Linq.IOrderedQueryable(中   LINQPad.User.Events)的'定义   System.Linq.Queryable:数据类型(S)   的类型参数(多个)不能   根据这些参数推断。   指定数据类型(多个)明确   或许可以纠正此错误。       扩展方法公共功能的OrderBy(TKEY的中)(的KeySelectors作为   System.Func(LINQPad.User.Events中,   TKEY的))作为   System.Linq.IOrderedEnumerable(中   LINQPad.User.Events)的'定义   System.Linq.Enumerable:评论   不是一个构件   LINQPad.User.Events。       扩展方法公共功能的OrderBy(TKEY的中)(的KeySelectors作为   System.Func(LINQPad.User.Events中,   TKEY的))作为   System.Linq.IOrderedEnumerable(中   LINQPad.User.Events)的'定义   System.Linq.Enumerable:数据类型(S)   的类型参数(多个)不能   根据这些参数推断。   指定数据类型(多个)明确   或许可以纠正此错误。

编辑2

下面是生成的SQL

  DECLARE @ P0智力= 2

选择 *
FROM [DBO]。[事件] AS [T0]
ORDER BY(((
    SELECT SUM([T1]。[投票])
    FROM [DBO]。[EventVotes] AS [T1]
    WHERE [T1]。[事件ID] = [T0]。[ID]
    ))* @ P0)+((
    SELECT COUNT(*)
    FROM [DBO]。[评论] AS [T2]
    WHERE [T2]。[事件ID] = [T0]。[ID]
    ))
 

解决方案

你的意思是 OrderByDescending ? ;)

I'm trying to create a linq expression whereby I can COUNT the number of event comments as well as SUM the number of Votes. Here's what I've got, but I'm not getting the appropriate results.

My example is done in VB.Net, but I can deal with examples in C# as well.

    Public Function GetHotEvents(ByVal skip As Integer) As List(Of Domain.Event) Implements IEventService.GetHotEvents
        ''# Our order by sequence just takes the number of votes and multiplies
        ''# it by two. Then takes the number of comments and adds it to the
        ''# vote count.  This way up-voted events will climb to the top while
        ''# down-voted events will fall to the bottom.  Comments also add to
        ''# the "hotness" of the event.
        Return _EventRepository.Read() _
            .Where(Function(e) e.EventDate >= Date.Today) _
            .OrderBy(Function(e) (((e.EventVotes.Sum(Function(s) s.Vote)) * 2) + (e.Comments.Count))) _
            .Skip(skip) _
            .Take(5) _
            .ToList()
    End Function

What I've done to test this is have ZERO comments on all events, and upvote ONE event. That event "should" float to the top, but it's not there.

Any help would be greatly appreciated.

edit

I've tried building the expression in LinqPad but unfortunately it threw an error (note: this error doesn't get thrown in my code)

Overload resolution failed because no accessible 'OrderBy' can be called with these arguments: Extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Linq.Expressions.Expression(Of System.Func(Of LINQPad.User.Events, TKey))) As System.Linq.IOrderedQueryable(Of LINQPad.User.Events)' defined in 'System.Linq.Queryable': 'Comments' is not a member of 'LINQPad.User.Events'. Extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Linq.Expressions.Expression(Of System.Func(Of LINQPad.User.Events, TKey))) As System.Linq.IOrderedQueryable(Of LINQPad.User.Events)' defined in 'System.Linq.Queryable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. Extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Func(Of LINQPad.User.Events, TKey)) As System.Linq.IOrderedEnumerable(Of LINQPad.User.Events)' defined in 'System.Linq.Enumerable': 'Comments' is not a member of 'LINQPad.User.Events'. Extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Func(Of LINQPad.User.Events, TKey)) As System.Linq.IOrderedEnumerable(Of LINQPad.User.Events)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.

edit 2

Here's the generated SQL

DECLARE @p0 Int = 2    

SELECT *
FROM [dbo].[Events] AS [t0]
ORDER BY (((
    SELECT SUM([t1].[Vote])
    FROM [dbo].[EventVotes] AS [t1]
    WHERE [t1].[EventID] = [t0].[ID]
    )) * @p0) + ((
    SELECT COUNT(*)
    FROM [dbo].[Comments] AS [t2]
    WHERE [t2].[EventID] = [t0].[ID]
    ))

解决方案

Did you mean OrderByDescending? ;)

这篇关于LINQ:多个字段排序依据总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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