您能告诉我如何将以下SQL代码转换成SQL或Linq到Entites吗?
正确的SQL代码是:
从收集组中选择CollectId、url、userid、公共时间(通过Collect.pubtime从收集d中选择d.url = collect.url )。
数据库表脚本是:
如果存在
(从sysobjects选择*,其中id =OBJECT_ID( 'IsUserTable')和OBJECTPROPERTY(id,‘IsUserTable’)= 1)
创建表Collect
使用NOCHECK添加约束PK_Collect主键的ALTER收集( CollectId )集IDENTITY_INSERT收集
插入Collect值( 1,'www.sohu.com','Mike','2008-10-10 :00:00‘)插入Collect值( 2,'www.echina365.com',’莉莉‘,’2008-10-150:00:00‘)插入Collect值( 3,'www.php.com',’Collect‘,’08-10-20:00:00‘)插入Collect值( 4,插入Collect值( 5,www.echina365.com,'Mike',‘2008-10-250:00:00’)插入Collect值( 6,'www.sohu.com','Jack','2008-10-26 0:00:00')插入Collect值( 7,'www.echina365.com','Tracy',)插入Collect值( 8,'www.php.com','YaoMing','2008-11-5 :00:00‘)
设置IDENTITY_INSERT从收集
发布于 2008-11-26 05:21:08
因为您的“有”条件实际上不在聚合列上,所以不能只使用"where“子句吗?
select distinct CollectId, url, userid, pubtime
from Collect
where pubtime >= (select max(pubtime) from collect d where d.url = collect.url)
order by Collect.pubtime desc考虑到您提供的数据集,这将得到相同的结果。然后LINQ语句变得相当简单:
var rows = (from c in Collect
where c.PubTime >= (
from d in Collect
where d.Url == c.Url
select d.PubTime).Max()
orderby c.PubTime descending
select c).Distinct();不过,我可能误解了你的意图。也许我的查询版本没有完全按照您的要求执行。如果是这样,请给我留下评论,我会删除答案,以免混淆问题。
https://stackoverflow.com/questions/319800
复制相似问题