2

我一直在 Silverlight 应用程序中使用 Linq 查询,它只返回包含字段 OptionARMRunId(身份)的最大值的表行。在 LinqPad 中执行时,查询运行良好并返回正确的行。但是,当在我的 Silverlight 应用程序中使用时,应用程序永远不会越过加载屏幕(当状态百分比变为 100% 时,蓝色圆圈继续旋转,令人作呕)并且我在浏览器中收到错误消息。我已经包含了原始的 Linq 语句,它出现在我的查询中的语句,以及下面的 ie 错误。

Linq 语句(正常工作):

    from OptionARMProjection in OptionARMProjections.Where(row => row.OptionARMRunId == OptionARMProjections.Max(r => r.OptionARMRunId))
select OptionARMProjection

C# 类中的 Linq 语句(运行 silverlight 应用程序时导致错误):

    crocodileEntities proxy = new crocodileEntities(new Uri("CrocodileDbDataService.svc", UriKind.Relative));



    var ProjectionsQuery = from OptionARMProjections in proxy.OptionARMProjections.Where(row => row.OptionARMRunId == proxy.OptionARMProjections.Max(r => r.OptionARMRunId))
                            select OptionARMProjections;

在 ie8 中收到错误:

网页错误详情

用户代理:Mozilla/4.0(兼容;MSIE 8.0;Windows NT 6.1;Trident/4.0;SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729;.NET CLR 3.0.30729;Media Center PC 6.0;InfoPath。 2;Zune 4.0;OfficeLiveConnector.1.4;OfficeLivePatch.1.3;.NET4.0C;.NET4.0E) 时间戳:2010 年 1 月 20 日星期三 03:06:13 UTC

消息:Silverlight 2 应用程序中的未处理错误不支持方法“Max”。在 System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce) 在 System.Data.Services.Client.ExpressionVisitor.Visit(Expression exp) 在 System.Data.Services.Client.DataServiceExpressionVisitor.Visit(Expression exp) 在 System .Data.Services.Client.ExpressionVisitor.VisitBinary(BinaryExpression b) 在 System.Data.Services.Client.ResourceBinder.VisitBinary(BinaryExpression b) 在 System.Data.Services.Client.ExpressionVisitor.Visit(Expression exp) 在 System.Data System.Data.Services.Client.ExpressionVisitor.VisitLambda(LambdaExpression lambda) 在 System.Data.Services.Client.ExpressionVisitor 的 .Services.Client.DataServiceExpressionVisitor.Visit(Expression exp)。1 original) at System.Data.Services.Client.ExpressionVisitor.VisitMethodCall(MethodCallExpression m) at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce) at System.Data.Services.Client.ExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.DataServiceExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.ResourceBinder.AnalyzeProjection(MethodCallExpression mce, Boolean matchMembers, Expression& e) at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce) at System.Data.Services.Client.ExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.DataServiceExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.ResourceBinder.Bind(Expression e) at System.Data.Services.Client.DataServiceQueryProvider.Translate(Expression e) at System.Data.Services.Client.DataServiceQuery1.get_QueryComponents() 在 System.Data.Services.Client.DataServiceRequest.CreateResult(对象源,DataServiceContext 上下文,AsyncCallback 回调,对象状态)在 System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback 回调,对象状态) 在 OptionARMChart.OptionARMUniverse.GetOptionArmProjectionsASync() 在 OptionARMChart.MainPage..ctor()
在 OptionARMChart.App.Application_Startup(Object sender, StartupEventArgs e) 在 MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName) 行:1 字符:1 代码:0 URI: http://localhost:5004/optionarmcharttestpage.aspx

4

2 回答 2

3

.Max() 不受支持,解决方法是执行相反的顺序并取第一个:

var ProjectionsQuery = proxy.OptionARMProjections
                       .Where(row => row.OptionARMRunId == proxy.OptionARMProjections
                       .OrderByDescending(r => r.OptionARMRunId))
                       .Take(1);

更正:似乎 Silverlight 3+ 支持它,但它不止一次给我同样的麻烦,所以也许有人可以添加更多细节来说明原因。

于 2010-01-20T03:08:50.257 回答
1

数据服务使用的 URI 语法不支持 LINQ to Objects 或 LINQ to SQL 支持的所有函数。我假设当您在 LINQPad 中查询它时,您正在执行 LINQ to SQL 查询,这就是它可以在那里工作的原因。但是,数据服务不支持查询的复杂性,这在这种情况下解释了您的运行时异常。

从您的查询来看,您应该可以使用 Nick 提出的解决方案,但我认为他提供的 Where 子句中存在错误。实际上,从您的原始示例中,您甚至不需要 Where 子句:

var ProjectionsQuery = proxy.OptionARMProjections .OrderByDescending(row => row.OptionARMRunId) .Take(1);

于 2010-01-21T02:27:39.350 回答