【问题标题】:Getting an Exception "System.ArgumentException was unhandled by user code" while converting ObservableCollectionView into AsQueryable ()将 ObservableCollectionView 转换为 AsQueryable () 时出现异常“用户代码未处理 System.ArgumentException”
【发布时间】:2014-11-10 05:16:28
【问题描述】:

我使用了 ObservableCollection 类型的 object 。我尝试通过以下代码进行过滤

public ObservableCollectionView<Person> SampleEntries { get; set; }


private void ApplyFilter(int age)
    {
        SampleEntries.Filter = (entry => entry.Age > age ) ;
        // converting ObservableCollection into AsQueryable
        var source = this.SampleEntries.AsQueryable();
        //Shows the value as Destination array was not long enough
        var source1 = source.OrderByDescending(x => x.Id);

    }

应用过滤器后,尝试对列进行排序,它抛出了

Exception  : "System.ArgumentException was unhandled by user code" Message=Destination array was not long enough. Check destIndex and length, and the array's lower bounds.

注意:我需要知道为什么这段代码不起作用。我们已经有其他方法来解决这个问题。

更新:ObservableCollectionView 类可以在 MyToolkit 库中找到。

【问题讨论】:

    标签: c# linq winrt-xaml mytoolkit


    【解决方案1】:

    在我看来,ObservableCollectionView 并不是为直接与 Linq 一起使用而设计的。如果您只需要按降序对 ObservableCollectionView 中的项目进行排序,则应使用 OrderAscending 属性。例如:

        ...
        SampleEntries.Filter = (entry => entry.Age > age ) ;
        SampleEntries.Order= x => x.Id;
        SampleEntries.Ascending = false; 
        ...
    

    如果你真的需要使用 Linq,试试这个:

        ...
        var source = this.SampleEntries.AsQueryable().ToList();
        var source1 = source.OrderByDescending(x => x.Id);
        ...
    

    【讨论】:

    • 感谢您的回复。我们已经将输入用作 IEnumerable 类型。我们将其转换为 AsQueryable 用于我们的流程。在这里,您告诉再次转换回 IEnumerable 作为 ToList()。同样我们已经尝试过上述方法,我们需要知道为什么 AsQueryable 的条目会引发异常,我们需要更新我们的客户并提供正当理由。
    • @user2826004 如果你想知道它不工作的确切原因,你可以下载MyToolkit的源代码并调试它。
    【解决方案2】:

    您是否先对集合进行了初始化?

    【讨论】:

    • 我将集合实例化为 SampleEntries=GetSampleData();私有 ObservableCollectionView GetSampleData() { ObservableCollectionView sampleSource = new ObservableCollectionView(); for (int i = 1; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    相关资源
    最近更新 更多