2013-03-25 58 views
1

我有这张表的基金数据值,我想选择每个基金(FundId)的最新值。 这个查询虽然在执行时给了我一些问题。 “不支持指定的方法”。LINQ查询,通过分组选择最新值 - 不支持的方法

var q = from f in ctx.FundDatas 
group f by f.FundId into g 
let latestDataItem = g.OrderByDescending(r => r.DateOfValue).FirstOrDefault() 
select new { 
    g.Key, LatestDataItem = latestDataItem 
}; 
var list = q.ToList(); //Executed and exception is thrown 

为什么不按这个顺序工作?我只想拿到钥匙和DateOfValue,如果是这样我会只跳过了“让”的一部分,并作出选择是这样的:

select new { 
    g.Key, 
    LatestDateOfValue = g.Max(y=>y.DateOfValue) 
}; 

上述作品...但我想每个基金数据项最新的全部对象,而不仅仅是最大日期。

这里的内部异常堆栈跟踪:

[NotSupportedException: Specified method is not supported.] 
MySql.Data.Entity.SqlGenerator.Visit(DbApplyExpression expression) +28 
System.Data.Common.CommandTrees.DbApplyExpression.Accept(DbExpressionVisitor`1 visitor)  +25 
MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name,  TypeUsage type) +35 
MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +21 
MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +38 
System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +25 
MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree) +60 
MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) +376 
System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree) +125 
System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) +442 

与.NET连接器6.5.4.0我运行MySQL。

编辑: 表定义:

FundId  int(6), PK 
DateOfValue date, PK 
Value  double(12,6) 
+0

“我有这张表有基金数据值” - 哪张表?你是否想要包含表格定义? – Sepster 2013-03-25 07:31:59

+0

在那里,添加了表格定义。这是一个非常简单的表,其中FundId和DateOfValue作为关键字,然后将实际值作为double。 – Andreas 2013-03-25 07:40:09

+0

对不起 - 并不是暗示它是需要的,只是看起来好像你打算包含它。 – Sepster 2013-03-25 08:00:58

回答

0

有一个在上FirstOrDefault()方法连接的x.5.4版本已知的bug。

参考http://bugs.mysql.com/bug.php?id=67377

While executing the following LINQ. It used to work in the version 6.3.5: 
entities.Reclamation.Where(x=> x.idUser == idUser).Select(x=> new { 
    id = x.id, 
    ReclamationReport = x.Report.Count == 0 ? null : x.Report.FirstOrDefault().id 
    }); 

我不知道如果这是你的问题的原因,因为臭虫记者似乎没有使用相同的模式,你(尽管我的Linq不是很好,所以我甚至不知道我是否将苹果与苹果进行比较),虽然这个例外的意思是相似的,但它并不完全相同(尽管在版本6.6.4中报告了错误,所以可能会解释你在v6.5.4中看到的细微差别)。

请参阅此维护版本的文章,对适用的版本到6.5.4:

https://blogs.oracle.com/MySqlOnWindows/entry/mysql_connector_net_6_55

修复的方法FirstOrDefault没有一些支持LINQ to Entities查询(MySQL的BUG#67377,甲骨文错误#15856964)。

所以,我建议应用维护版本。

+0

谢谢!我试图更新到最新的GA版本,它的工作原理。但是,我直接遇到了其他问题。 'where子句'中的未知列'Extent1.FundId'。我现在在LinqPad中测试了我的LINQ查询,并且完美运行。无论是mysql连接器还是我的项目都必须有一些非常错误的东西。 – Andreas 2013-03-25 10:14:04

+0

@Andreas我的配偶很高兴。感谢你的接纳!我有一个非常好的支持历史,并且挖掘这种东西。听起来好像是个好消息,虽然......我对.NET的mySql没有实际经验,Linq也无知,对不起......所以我会建议提出一个新问题,提供关于你的新问题的细节,并且与正确的标题和标签,你会得到你需要这个新问题的帮助。祝你好运! – Sepster 2013-03-25 11:03:50