使用Linq获取字段中具有重复值的最后N个行

编程入门 行业动态 更新时间:2024-10-09 17:19:37
本文介绍了使用Linq获取字段中具有重复值的最后N个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定一个数据库表,一个列名C和一个大于1的数字N,我如何才能得到一组具有等于C列且至少具有N行的值的行?如果存在多个这样的组,则需要获取包含最新条目(具有最大ID的组)的组.

Given a database table, a column name C, and a number N larger than 1, how can I get a group of rows with equal values of column C which has at least N rows? If there exists more than one such group, I need to get the group which contains the newest entry (the one with the largest Id).

是否可以使用LINQ to Entities来做到这一点?

Is it possible to do this using LINQ to Entities?

Example: > Id | Mycolumn > - - - - - - - > 1 | name55555 > 2 | name22 > 3 | name22 > 4 | name22 > 5 | name55555 > 6 | name55555 > 7 | name1 Primary Key: ID OrderBy: ID Repeated column: Mycolumn

如果N = 3和C = Mycolumn,那么我们需要获取具有MyColumn列重复至少3次的行.

If N = 3 and C = Mycolumn, then we need to get rows which have the column MyColumn duplicated at least 3 times.

对于上面的示例,它应该返回第1、5和6行,因为name55555的最后一个索引是6,而name22的最后一个索引(也重复了3次)是4.

For the example above, it should return rows 1, 5 and 6, because last index of name55555 is 6, and last index of name22 (which is also repeated 3 times) is 4.

推荐答案

data.Mytable .OrderByDescending(m => m.Id) .GroupBy(m => m.Mycolumn) .FirstOrDefault(group => group.Count() >= N) .Take(N) .Select(m => m.Id)

更多推荐

使用Linq获取字段中具有重复值的最后N个行

本文发布于:2023-11-30 15:41:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!