在Queryable类中使用Count方法时出现异常

时间:2013-05-23 13:30:21

标签: c# count windows-runtime iqueryable

WinRT 中,当我在Queryable类中为IOrderedEnumerable实例调用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

2 个答案:

答案 0 :(得分:2)

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...
}

除非您使用非常大的集合,否则性能不会太差。

答案 1 :(得分:0)

您必须使用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

相关问题