0

我希望有人能指出我正确的方向;我想我已经接近了,但我从来没有做过线程并且一直停留在语法/概念上。我相信第一部分正在工作,但是我有一个关于线程所有者的可爱 WPF 错误。我知道不知何故我已经用 Dispatcher 或 BackGroundWorker 解决了这个问题,但时间越来越短了——今晚我要碰壁,无法独自解决。这是我失败的部分:

 Private Sub FilterData(viewSource As CollectionViewSource, Filter As Expression(Of Func(Of person, Boolean)))
    _repository.GetFilteredPersons(Filter)

    viewSource.Source = _repository.FilteredPersons
End Sub

现在下一点,我相信要么是 A) 工作,要么是 B) 愚弄我,因为我还没有证明:)。

 Public Property FilteredPersons() As ObservableCollection(Of Person)


    Public Async Sub GetFilteredPersons(criteria As Expression(Of Func(Of Person, Boolean)))
        FilteredPersons = Await FilterPersons(criteria)
    End Sub

    Private Async Function FilterPersons(criteria As Expression(Of Func(Of Person, Boolean))) As Task(Of ObservableCollection(Of Person))
        Return Await Run(Function()
                             Dim res = New ObservableCollection(Of Person)

                             _context = New caredocsSQLEntities
                             Dim test = _context.Persons.Where(criteria).Select(Function(o) New With { _
                                  .Person = o, _
                                  .events = o.events.Where(Function(e) e.eventDateTime >= Startdate And e.eventDateTime <= Enddate).OrderByDescending(Function(x) x.eventDateTime)
                             })

                             For Each t In test
                                 res.Add(t.Person)
                             Next
                             Return res
                         End Function
        )
    End Function

最后,确切的错误是The calling thread cannot access this object because a different thread owns it

一旦我有机会再次吃饭和阅读,我将在几个小时内再次解决这个问题。但是,如果有人能告诉我我没有完全理解的明显愚蠢,我将不胜感激!

上下文是具有子导航过滤属性的 Entity Framework 5 ObservableCollection - 我认为代码并没有说清楚:)。

谢谢 :)。

根据要求,我得到了例外。这发生在存储库代码中。线程的东西是一个红鲱鱼。

System.InvalidOperationException occurred
HResult=-2146233079
Message=The calling thread cannot access this object because a different thread owns it.
Source=System.Data.Entity
StackTrace:
   at System.Data.Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments)
   at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
   at                   System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at SomeEntity.Models.PersonRepository._Closure$__2._Lambda$__7() in E:\Dropbox\Work         Experimental\SomeEntity - Copy\SomeEntity\Models\PersonRepository.vb:line 30

内部异常:

这是整个异常,它发生在这里:

  For Each t In test
          res.Add(t.Person)
  Next

我昨晚浏览了这些文章,虽然我对它的理解更好一些,但现在我仍在努力将它应用于存储库类。这通常是语法混乱的组合,而我并没有完全理解。我有一种感觉,我应该能够将 getfiltered 居民中的部分操作拆分为 await 任务,但还没有设法弄清楚。今天的工作——如果实体框架可以这样使用!

4

0 回答 0