繁体   English   中英

获取Linq中最重复记录的计数

[英]Getting the count of most repeated records in Linq

我正在开发一个应用程序,其中必须将歌曲的播放历史记录存储在数据表中。 我有一个名为PlayHistory的表,该表有四列。

Id | SoundRecordingId(FK) | UserId(FK) | DateTime

现在,我必须实现一个查询,该查询将返回处于流行阶段(即主要播放)的歌曲。 我在sql服务器中编写了以下查询,该查询以某种方式返回我的数据,使其更接近我想要的数据。

select  COUNT(*) as High,SoundRecordingId
from PlayHistory
where DateTime >= GETDATE()-30
group by  SoundRecordingId
Having COUNT(*) > 1
order by SoundRecordingId desc

它返回了以下数据:

High  SoundRecordingId
2       5
2       3

这意味着编号为5和3的歌曲的播放次数最多。ie2如何在C#中通过Linq实现此功能。 到目前为止,我已经做到了:

        DateTime d = DateTime.Now;
        var monthBefore = d.AddMonths(-1);
        var list =
            _db.PlayHistories
            .OrderByDescending(x=>x.SoundRecordingId)
            .Where(t => t.DateTime >= monthBefore)
            .GroupBy(x=>x.SoundRecordingId)
            .Take(20)
            .ToList();

它返回我整个表的列表,其中包含SoundRecording对象的数量,但我只想计数最重复的记录。 谢谢

我喜欢'linq'语法,它类似于SQL

var query = from history in _db.PlayHistories
            where history.DateTime >= monthBefore
            group history by history.SoundRecordingId into historyGroup
            where historyGroup.Count() > 1
            orderby historyGroup.Key
            select new { High = historyGroup.Count(), SoundRecordingId = historyGroup.Key };

var data = query.Take(20).ToList();

.GroupBy方法的重载将解决您的问题。

DateTime d = DateTime.Now;
    var monthBefore = d.AddMonths(-1);
    var list =
        _db.PlayHistories
        .OrderByDescending(x=>x.SoundRecordingId)
        .Where(t => t.DateTime >= monthBefore)

        .GroupBy(x=>x.SoundRecordingId, (key,values) => new {SoundRecordingID=key, High=values.count()})

        .Take(20)
        .ToList();

我只是将结果选择器添加到此处的GroupBy方法调用中,该选择器执行与您在SQL中编写的转换相同的转换。

有问题的方法重载记录在这里

为了进一步解决您的问题,您可能需要执行另一个OrderByDescending以使受欢迎程度顺序得到结果。 为了匹配SQL语句,您还必须仅过滤计数> 1。

DateTime d = DateTime.Now;
    var monthBefore = d.AddMonths(-1);
    var list =
        _db.PlayHistories
        .Where(t => t.DateTime >= monthBefore)
        .GroupBy(x=>x.SoundRecordingId, (key,values) => new {SoundRecordingID=key, High=values.count()})
        .Where(x=>x.High>1)
        .OrderByDescending(x=>x.High)
        .ToList();

大功告成。 只需按计数顺序排序列表,然后选择第一个即可:

var max =
        _db.PlayHistories
        .OrderByDescending(x=>x.SoundRecordingId)
        .Where(t => t.DateTime >= monthBefore)
        .GroupBy(x=>x.SoundRecordingId)
        .OrderByDescending(x => x.Count())
        .First();

这为您提供了一个键-值对,其中“ Key是您的SoundRecordingId而“值”是其在输入列表中的出现次数。

编辑:要获取具有该金额的所有记录,请选择此代替:

var grouped =
        _db.PlayHistories
        .OrderByDescending(x => x.SoundRecordingId)
        .Where(t => t.DateTime >= monthBefore)
        .GroupBy(x => x.SoundRecordingId)
        .Select(x => new { Id = x.Key, Count = x.Count() }
        .OrderByDescending(x => x.Count)
        .ToList();
var maxCount = grouped.First().Count;
var result = grouped.Where(x => x.Count == maxCount);

通过为您提供所需的内容,可以解决问题。 在LINQ中查询,仅返回播放计数。

var list = _db.PlayHistories.Where(x => x.DateTimeProp > (DateTime.Now).AddMonths(-1))
           .OrderByDescending(y => y.SoundRecordingId.Count())
           .ThenBy(z => z.SoundRecordingId)
           .Select(xx => xx.SoundRecordingId).Take(20).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM