异常时,我使用计数方法在Queryable类

本文关键字:方法 Queryable 异常 | 更新日期: 2023-09-27 18:14:57

WinRT中,当我为IOrderedEnumerable实例调用Queryable类中的count方法时,它将抛出异常。

DoWork(emp); //Working fine 
DoWork(emp.OrderBy(objects => objects.EmployeeId)); //throw exception..
public void DoWork(IEnumerable<object> collection)
{
    var queryable = collection.AsQueryable();
    int count = 0;
    if (queryable != null)
        count = queryable.Count();
    else
        throw new InvalidOperationException("Not able to get count");
    //Some other operations using queryable...
}

异常

System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=The API 'System.Linq.OrderedEnumerable`2[[SfDataGrid.BusinessObjects, SfDataGrid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' cannot be used on the current platform. See http://go.microsoft.com/fwlink/?LinkId=248273 for more information.
Source=mscorlib
StackTrace:
at System.Reflection.Emit.DynamicILGenerator.GetTokenFor(RuntimeType rtType)
at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, Type type)
at System.Linq.Expressions.Compiler.BoundConstants.EmitConstantFromArray(LambdaCompiler lc, Object value, Type type)
at System.Linq.Expressions.Compiler.BoundConstants.EmitConstant(LambdaCompiler lc, Object value, Type type)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitConstant(Object value, Type type)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitConstantExpression(Expression expr)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitExpression(Expression node, CompilationFlags flags)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitArguments(MethodBase method, IArgumentProvider args, Int32 skipParameters)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitMethodCall(MethodInfo mi, IArgumentProvider args, Type objectType, CompilationFlags flags)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitMethodCall(Expression obj, MethodInfo method, IArgumentProvider methodCallExpr, CompilationFlags flags)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitMethodCallExpression(Expression expr, CompilationFlags flags)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitExpression(Expression node, CompilationFlags flags)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitLambdaBody(CompilerScope parent, Boolean inlined, CompilationFlags flags)
at System.Linq.Expressions.Compiler.LambdaCompiler.EmitLambdaBody()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Linq.EnumerableExecutor`1.Execute()
at System.Linq.EnumerableQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at SfDataGrid.ViewModel.GetCount(IEnumerable`1 collection) in e:'DiskD'Support'I108101'SfDataGrid1022101733'SfDataGrid'SfDataGrid'ViewModel'ViewModel.cs:line 39
at SfDataGrid.ViewModel..ctor() in e:'DiskD'Support'I108101'SfDataGrid1022101733'SfDataGrid'SfDataGrid'ViewModel'ViewModel.cs:line 27
at SfDataGrid.SfDataGrid_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_ViewModel() in e:'DiskD'Support'I108101'SfDataGrid1022101733'SfDataGrid'SfDataGrid'obj'Debug'XamlTypeInfo.g.cs:line 123
at SfDataGrid.SfDataGrid_XamlTypeInfo.XamlUserType.ActivateInstance() in e:'DiskD'Support'I108101'SfDataGrid1022101733'SfDataGrid'SfDataGrid'obj'Debug'XamlTypeInfo.g.cs:line 3679

异常时,我使用计数方法在Queryable类

似乎IOrderedEnumerable<T>.AsQueryable()是使用WinRT中不可用的动态代码生成实现的。您可以通过在调用AsQueryable():

之前将其具体化到列表中来解决这个问题。
public void DoWork(IEnumerable<object> collection)
{
    var queryable = collection.ToList().AsQueryable();
    int count = 0;
    if (queryable != null)
        count = queryable.Count();
    else
        throw new InvalidOperationException("Not able to get count");
    //Some other operations using queryable...
}

除非处理的是非常大的集合,否则性能应该不会差太多。

必须使用IOrderable<object>而不是IEnumerable<object>作为DoWork方法的参数

IOrderable:

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.utilities.iorderable.aspx

IEnumerable:

http://msdn.microsoft.com/en-us/library/system.collections.ienumerable (v = vs.100) . aspx