gpt4 book ai didi

c# - LINQ 表达式适用于 LinqPad 但不适用于 C# Silverlight 应用程序

转载 作者:行者123 更新时间:2023-11-30 21:22:52 26 4
gpt4 key购买 nike

我一直在 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 中收到错误:

Webpage error details

User Agent: Mozilla/4.0 (compatible; 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) Timestamp: Wed, 20 Jan 2010 03:06:13 UTC

Message: Unhandled Error in Silverlight 2 Application The method 'Max' is not supported. 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.ExpressionVisitor.VisitBinary(BinaryExpression b) at System.Data.Services.Client.ResourceBinder.VisitBinary(BinaryExpression b) at System.Data.Services.Client.ExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.DataServiceExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.ExpressionVisitor.VisitLambda(LambdaExpression lambda) at System.Data.Services.Client.ExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.DataServiceExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.ExpressionVisitor.VisitUnary(UnaryExpression u) at System.Data.Services.Client.ExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.DataServiceExpressionVisitor.Visit(Expression exp) at System.Data.Services.Client.ExpressionVisitor.VisitExpressionList(ReadOnlyCollection1
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.DataServiceQuery
1.get_QueryComponents() at System.Data.Services.Client.DataServiceRequest.CreateResult(Object source, DataServiceContext context, AsyncCallback callback, Object state) at System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback callback, Object state) at OptionARMChart.OptionARMUniverse.GetOptionArmProjectionsASync() at OptionARMChart.MainPage..ctor()
at OptionARMChart.App.Application_Startup(Object sender, StartupEventArgs e) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName) Line: 1 Char: 1 Code: 0 URI: http://localhost:5004/optionarmcharttestpage.aspx

最佳答案

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

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

更正:Seems it is supported in Silverlight 3+ ,但它不止一次给我同样的麻烦,所以也许有人可以添加更多关于原因的细节。

关于c# - LINQ 表达式适用于 LinqPad 但不适用于 C# Silverlight 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2098790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com