简体   繁体   English

LINQ查询,选择分组依据的最新值-不支持的方法

[英]LINQ query, selecting latest value with group by - Unsupported method

I have this table with fund data values and I want to select the latest values for each fund (FundId). 我有此表,其中包含基金数据值,我想选择每个基金的最新值(FundId)。 This query is giving me problems though when executed. 该查询在执行时给我带来了问题。 "Specified method is not supported". “不支持指定的方法”。

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

Why wouldn't this order by work? 为什么不按工作顺序下达此命令? I don't want to just get the Key and DateOfValue, if so I would have just skipped the "let" part and made the select like this: 不想只拿到钥匙和DateOfValue,如果是这样我会只跳过了“让”的一部分,并作出选择是这样的:

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

The above works... But I want the whole object of the latest of each fund data items, not just the max date. 上面的作品...但是我想要每个基金数据项目中最新的整个对象,而不仅仅是最大日期。

Here's the inner exception stack trace: 这是内部异常堆栈跟踪:

[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

I'm running MySQL with .NET connector 6.5.4.0. 我正在运行带有.NET连接器6.5.4.0的MySQL。

Edit: Table definition: 编辑:表定义:

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

There's a known bug in the x.5.4 versions of the connection on the FirstOrDefault() method. FirstOrDefault()方法上的连接的x.5.4版本中存在一个已知的错误。

Refer http://bugs.mysql.com/bug.php?id=67377 请参阅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
    });

I'm not sure if this is the cause of your issue, as the bug reporter doesn't appear to be using the same pattern as you (although my Linq is not up to scratch so I don't even know if I'm comparing apples with apples), and while the exception is similar in implied meaning it is not identical (although bug reported in v6.6.4, so that might explain the subtle difference to what you're seeing in v6.5.4). 我不确定这是否是造成您问题的原因,因为错误报告者似乎并没有使用与您相同的模式(尽管我的Linq并没有完全解决,所以我什至不知道我是否比较苹果与苹果),虽然隐含的异常相似,但并不完全相同(尽管v6.6.4中报告了错误,因此可能解释了与v6.5.4中所见内容的细微差别)。

Refer to this maintenance release article, for the release applicable to 6.5.4: 请参阅此维护版本文章,以了解适用于6.5.4的版本:

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

Fix for method FirstOrDefault not supported in some LINQ to Entities queries (MySql bug #67377, Oracle bug #15856964). 修复了某些LINQ to Entities查询中不支持方法FirstOrDefault的问题(MySql错误#67377,Oracle错误#15856964)。

So, I would recommend applying the maintenance release. 因此, 我建议您应用维护版本。 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM