问题描述
我有一个很难理解我怎么能形成一个LINQ查询执行以下操作:
I'm having a hard time understanding how I can form a LINQ query to do the following:
我有一个表CallLogs,我想回到从而重新$ P $一个结果psents具有持续时间最长的呼叫。
I have a table CallLogs and I want to get back a single result which represents the call that has the longest duration.
该行看起来是这样的:
[ID] [RemoteParty] [时间]
可以有多个行为同一RemoteParty,其中每一个重新presents一个特定持续时间的呼叫。我想知道哪些RemoteParty具有最长的总时间。
There can be multiple rows for the same RemoteParty, each which represents a call of a particular duration. I'm wanting to know which RemoteParty has the longest total duration.
使用LINQ,我走到这一步:
Using LINQ, I got this far:
var callStats = (from c in database.CallLogs
group c by c.RemoteParty into d
select new
{
RemoteParty = d.Key,
TotalDuration = d.Sum(x => x.Duration)
});
所以,现在我有一个分组结果,总的持续时间为每个RemoteParty但我需要的最大单一结果。
So now I have a grouped result with the total duration for each RemoteParty but I need the maximum single result.
[DistinctRemoteParty1] [时间]
[DistinctRemoteParty2] [时间]
[DistinctRemoteParty2] [Duration]
[DistinctRemotePartyN] [时间]
[DistinctRemotePartyN] [Duration]
我怎样才能修改查询实现这一目标?
How can I modify the query to achieve this?
推荐答案
订单的结果,返回的第一个。
Order the result and return the first one.
var callStats = (from c in database.CallLogs
group c by c.RemoteParty into d
select new
{
RemoteParty = d.Key,
TotalDuration = d.Sum(x => x.Duration)
});
callStats = callStats.OrderByDescending( a => a.TotalDuration )
.FirstOrDefault();
这篇关于C#LINQ查询 - 分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!