- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试获取列中每个唯一项的 Top(X) 记录。
例子:
Inspections
InspectionId | RestaurauntName | InspectionDate
------------------------------------------------
1 Mom&Pop 10/10/12
2 SandwichesPlace 10/10/12
3 Mom&Pop 10/10/11
4 SandwichesPlace 10/10/11
5 Mom&Pop 10/10/10
6 SandwichesPlace 10/10/10
7 BurgerPlace 10/10/11
8 BurgerPlace 10/10/09
9 BurgerPlace 10/10/08
如果我将“2”的值作为参数传递,我希望返回记录 1、2、3、4、7、8。
在这个示例片段中,我试图为每个“restaurantName”获取一些记录。
public IQueryable<Inspections>
GetLatestInspectionDatesForAllRestaurants(int numRecords) {
IQueryable<Inspections> inspections= _session.Query<Inspections>()
.GroupBy(r=> r.RestaurauntName)
.SelectMany(g => g
.OrderBy(r => r.InspectionDate)
.Take(numRecords));
return inspections;
}
不幸的是,我遇到了这个错误。
Query Source could not be identified:
ItemName = g, ItemType = System.Linq.IGrouping`2
代码中此时触发异常
var inspections = _inspectionRepository.GetLatestInspectionDatesForAllRestaurants(2).ToList();
我错过了什么?
运行测试后,我确认查询在 LINQ-To-Objects 中运行良好。有没有可能 NHibernate.Linq 在这方面还不完善?我做了一个略有不同的查询(如下)并得到了一个未实现的异常。
.GroupBy(r=> r.RestaurauntName)
.Select(g => g.First()).Take(5)
如果我从末尾删除 Take(5),我会得到以下结果(提醒这是一个示例,在我的真实案例中,我的 PK 是 Id)。
{"Column '.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."}
我是否需要学习使用创建条件或其他东西?
最佳答案
好吧,我得到了我需要生成的 SQL,这需要一些研究等等。我确信我最初的问题与 LINQ 和 NHibernate 有关,尽管 Robert 发布的最后一个查询在技术上是正确的解决方法。
由于 IQueryable 的性质,只有在必要时才会枚举,这将生成单个 SQL 脚本。 SQL 本身生成“Where Exists”子句。
public IQueryable<Inspections>
GetLatestInspectionDatesForAllRestaurants(int numRecords)
{
var subQuery = _session.Query<Inspections>()
.OrderByDescending(x => x.InspectionDate);
var inspections = _session.Query<Inspections>()
.Where(x => subQuery.Where(y => y.InspectionDate ==
x.InspectionDate).Take(numRecords).Contains(x))
.OrderBy(x => x.WellDataId);
return inspections;
}
就是这样,希望它最终能帮助到其他人。
关于c# - 取 LINQ 中每个分组项目的 Top(X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17113012/
我有一个最大堆和一个最小堆,其中最大堆的最大元素小于或等于最小堆的最小元素。 我现在想移动最小堆的最小元素成为最大堆的最大元素。 一种方法是弹出最小堆的顶部元素并将其插入最大堆。 有没有更有效的方法来
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 3 年前。 Improve
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit th
我遇到过几篇(机器学习分类问题)期刊论文提到使用 Top-N 方法评估准确性。数据显示,在相同的训练、测试条件下,Top 1 准确率 = 42.5%,Top-5 准确率 = 72.5%。请问这个top
我是 js 和 vue 的初学者,我正在建立一个网站来自动化我的比萨店交付订单。 我有一个 v-for 浇头列表,客户可以从列表中最多选择 4 个浇头,他可以选择 2、3 或 4 次相同的浇头。 我为
Kubernetes top (kubectl top) 命令显示的内存使用情况与在 pod 内运行的 Linux top 命令不同。 我创建了 k8s 部署,其中 YAML 包含以下内存限制: re
linear-gradient() 的浏览器实现已经发展到支持 top 和 to top 作为 anchor 值。它们是同向还是相反? 最佳答案 它们相反。 to top 在元素底部使用第一个颜色参数
我有一个 HTML 对象: 1:1 但出于某种原因...当我通过以下代码在 jQuery 中访问它的 top 属性时: $tile = $('[data-x=1][data-y=1]');
这是我关于 stackoverflow 的第一个问题,如果我没有做对所有事情,请多多包涵。如果我可以更好地格式化它,请告诉我。 我正在完成 TOP 2nd CSS Margin/Padding 练习。
我正在尝试使我的登录页面与 css 保持一致。但是,我在两个属性 margin-top 和 top 之间遇到了问题。 margin-top倾向于根据第一个元素推送登录页面,而top只是登录页面的位置。
我正在构建一个 jQuery 表单字段验证器,目前我正致力于在 CSS 中构建弹出通知。 问题是,无论 margin 的应用如何,我都无法让 .notification-point 与 .notifi
假设我们有 3 个方法:方法 2 从方法 1 调用,方法 3 从方法 2 调用。方法 2 和 3 的大小均为 30 个字节码。此外,为了确定性,假设方法 2 总是从方法 1 调用一次,而方法 3 总是
我有几千条记录,我需要分批更新它们,每批更新 350 条记录。 我想知道以下两个更新语句是否有任何区别,其中一个是否会运行得更快,使用更少的数据库资源等。 语句1: UPDATE TOP (350)
目前我正在为一个元素开发一个 PSD 设计,在那个设计中,一个部分有一个完全不同的形状来吸引人的外观。 为了创建该形状,我创建了一个 svg 文件并将其用作背景并通过 absolute 属性定位该部分
我有 sticky-top 类的元素侧边栏和 div: .... 当侧边栏是粘性的时候,我需要传递一个边距,因为 sticky-t
我不希望他们处于同一水平。它们都是独立的div。侧边栏是固定的。为什么侧边栏不粘在顶部而不是与文章 div 对齐? body{ margin: 0; padding: 0; } div
似乎最好只为顶级函数创建一次 FunPtr,而不是在需要时创建一个新的函数(同一函数)并处理其释放。 除了 foreign import ccall "wrapper" 之外,我是否忽略了获取 Fun
希望得到一些指点,我有一个问题:我需要向具有 Clusterrole:view 的用户添加什么角色/权限才能让该用户在 kubectl 中使用“top node”和“top pod”命令?我们在 1.
我的情况非常简单: 我有一个名为 FullTextPagina 的表值函数,定义如下: select * from Pagina as p where contains(p.PageText, @te
这个问题已经有答案了: Use variable with TOP in select statement in SQL Server without making it dynamic [dupli
我是一名优秀的程序员,十分优秀!