Linq 查询:MAX in WHERE
我有一个很长的查询,返回
Item {
DateTime entryDate
.....
}
我喜欢将此查询的结果与另一个表结合起来
Value {
DateTime date,
double value
}
,这样如果entryDate >= CUTOFF,则取CUTOFF上的值,否则取entryDate上的值。换句话说,我想实现:
SELECT Item.*, Value.value WHERE
MIN( Item.entryDate, CUTOFF ) == Value.date
请原谅我的语法,但这就是想法。
编辑:经过一番试验和错误,我想出了这个 linq-to-sql 查询:
from iValue in Values
join iItem in ... (long query)
let targetDate = iItem.EntryDate > CUTOFF ? iItem.EntryDate : CUTOFF
where iValue.Date == targetDate
select new
{
iItem,
targetDate,
iValue
}
感谢您的帮助。
I have a long query that returns
Item {
DateTime entryDate
.....
}
I like to combine the result of this query with another table
Value {
DateTime date,
double value
}
such that if entryDate >= CUTOFF, then take the value on CUTOFF, else take the value on entryDate. In other words, I'd want to achieve:
SELECT Item.*, Value.value WHERE
MIN( Item.entryDate, CUTOFF ) == Value.date
Excuse my syntax, but that's the idea.
EDIT: After some trial and error, I came up with this linq-to-sql query:
from iValue in Values
join iItem in ... (long query)
let targetDate = iItem.EntryDate > CUTOFF ? iItem.EntryDate : CUTOFF
where iValue.Date == targetDate
select new
{
iItem,
targetDate,
iValue
}
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
过滤查询,将两项合并为一项并修改第一项
Filter the query, Combine the two items into one item and modify the first item
鉴于您已从
Item
查询返回数据,并且Value
表相对较小,那么这是一个不错的方法:ToLookup 扩展非常有用,但经常被忽视。
Given that you've returned your data from your
Item
query and that theValue
table is relatively small then this is a nice way to go:The
ToLookup
extension is very useful and often overlooked.这是矛盾的 -
if entryDate >= CUTOFF then take the value on CUTOFF
意味着您想要MIN(Item.entryDate, CUTOFF)
,而不是最大
。话虽如此,您只想选择与查询的每个项目匹配的 value.Value 。每个项目都应该查找与您的 MAX(或者我认为是 MIN)语句相匹配的相关值。
这将返回匿名
{ Item, double }
对象的 IQueryable。如果您需要将其作为单个 SQL 语句执行,则需要进行一些重构。一个好的开始是将
matchingValue
交换到单个语句中。我面前没有 Visual Studio 来确认,但我不确定 Math.Min 函数是否已映射到 LINQ-to-SQL 中。我们假设事实并非如此。
我相信,如果您使用
.ToList()
执行它,它将解析为单个查询,但在我面前有一些工具之前无法确认。使用 SQL 探查器对其进行测试以确定。This is contradictory -
if entryDate >= CUTOFF then take the value on CUTOFF
imples that you wantMIN(Item.entryDate, CUTOFF)
, notMAX
.Having said that, you just want to select the value.Value that matches each item of your query. Each item should look up the relevant value which matches your MAX (or, I believe, MIN) statement.
This will return an IQueryable of anonymous
{ Item, double }
objects.If you require this to be a executed as a single SQL statement you'll need to do some refactoring. A good start is to swap
matchingValue
into a single statement.I don't have a Visual Studio in front of me to confirm, but I am not sure that the
Math.Min
function is mapped in LINQ-to-SQL. Let's assume it's not.I believe that will resolve to a single query if you execute it with a
.ToList()
but can't confirm until I have some tools in front of me. Test it with SQL profiler to be sure.