LINQ查询多个聚集 [英] LINQ query with multiple aggregates

查看:168
本文介绍了LINQ查询多个聚集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何创建相当于Linq到对象查询?

  SELECT MIN(CASE WHEN纸张类型=在THEN p.PunchTime ELSE NULL END)AS EarliestIn,
       MAX(CASE WHEN纸张类型=远,再p.PunchTime ELSE NULL END)AS LatestOUt
从冲压p
 

解决方案

您不能有效地选择多聚集在香草LINQ到对象。可以执行多个查询,当然,但根据数据源可能是低效率的。

我有一个框架,对付这种我称之为推LINQ - 它只是一个爱好(我和马克Gravell),但我们相信,它的工作原理pretty的好。它可作为 MiscUtil 的一部分,你可以在<一个读到它href="http://msmvps.com/blogs/jon_skeet/archive/2008/01/04/quot-push-quot-linq-revisited-next-attempt-at-an-explanation.aspx"相对=nofollow>就可以了我的博客文章的。

这看起来有点奇怪 - 因为你定义你想要的结果去为期货,然后通过查询推数据,然后检索结果 - 但一旦你得到你的头圆它,它的罚款。我很想听听你是如何得到它 - 如果你使用它,请寄给我的skeet@pobox.com

How would I create the equivalent Linq To Objects query?

SELECT MIN(CASE WHEN p.type = "In" THEN p.PunchTime ELSE NULL END ) AS EarliestIn,
       MAX(CASE WHEN p.type = "Out" THEN p.PunchTime ELSE NULL END ) AS LatestOUt
FROM Punches p

解决方案

You can't efficiently select multiple aggregates in vanilla LINQ to Objects. You can perform multiple queries, of course, but that may well be inefficient depending on your data source.

I have a framework which copes with this which I call "Push LINQ" - it's only a hobby (for me and Marc Gravell) but we believe it works pretty well. It's available as part of MiscUtil, and you can read about it in my blog post on it.

It looks slightly odd - because you define where you want the results to go as "futures", then push the data through the query, then retrieve the results - but once you get your head round it, it's fine. I'd be interested to hear how you get on with it - if you use it, please mail me at skeet@pobox.com.

这篇关于LINQ查询多个聚集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆