在 LINQ 中使用 MIN 函数
我有一个视图的 SQL 查询。这个视图
SELECT
[ID]
[Name],
[Category],
MIN(OrderDate) as OrderDate,
MIN(DeliveryDate) as DeliveryDate
GROUP BY
[ID], [Name], [Category]
FROM
[OrdersView]
我知道这个查询是正确的,因为我的 DBA 给了我它。但我需要将其转换为 LINQ。目前,我有以下问题:
var orders = from o in dataContext.OrdersView
group o by new { o.ID, o.Name, o.Category } into grouping
select new Order() {
ID = grouping.Key.ID,
Name = grouping.Key.Name,
Category = grouping.Key.Category,
OrderDate = ?,
DeliveryDate = ?
};
我的问题是我不知道如何在 OrderDate 和 DeliveryDate 上执行 MIN 函数并在 LINQ 查询中使用这些值。不幸的是,我无法更改数据库,因为它已锁定。有人可以指出我正确的方向吗?
谢谢!
I have a SQL Query of a view. This view
SELECT
[ID]
[Name],
[Category],
MIN(OrderDate) as OrderDate,
MIN(DeliveryDate) as DeliveryDate
GROUP BY
[ID], [Name], [Category]
FROM
[OrdersView]
I know this query is correct because my DBA gave it to me. But I need to convert it into LINQ. Currently, I have the following:
var orders = from o in dataContext.OrdersView
group o by new { o.ID, o.Name, o.Category } into grouping
select new Order() {
ID = grouping.Key.ID,
Name = grouping.Key.Name,
Category = grouping.Key.Category,
OrderDate = ?,
DeliveryDate = ?
};
My problem is I do not know how to execute the MIN function on the OrderDate and DeliveryDate and use those values in my LINQ query. Unfortunately, I cannot change the database because its locked down. Can someone please point me in the right direction?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OrderDate = grouping.Min(a => a.OrderDate)
DeliveryDate = grouping.Min(a => a.DeliveryDate)
编辑:使用
o< /code> 而不是意外地
分组
。那应该适合你。OrderDate = grouping.Min(a => a.OrderDate)
DeliveryDate = grouping.Min(a => a.DeliveryDate)
Edit: Used
o
instead ofgrouping
accidentally. That should do it for you.将来我建议使用 LINQPad 来测试您的查询。它允许查看生成的 SQL 代码。
In future I'd recommend to use LINQPad for testing your queries. It allows to view generated SQL code.