0

I'm pretty stuck on this anonymous type from LINQ business. It's a consequence of working through an answer to a previous question, but sadly I just havent the brain power to get this to work at all.

I'm returning a limited list of events that are connected to each person. The source is Entity Framework where I'm still battling to get criteria limited results from a navigation property. The code below is my translation from Nicholas Butler's answer. However, I can't find any way to adapt the anonymous type. Casts don't seem to work - or I just haven't a clue (likely). Can anyone help please?

Dim test = context.persons.Where(Function (r) r.personActive).Select(Function (o) New With { _
                 .person = o, _
                 .events = o.events.Where( Function(e) e.eventDateTime > startdate) _
            })

            Dim Res = New ObservableCollection(Of person )

            For Each person In test
                Res.Add(person)
            Next

Gives me:

Error 2 Value of type '<anonymous type> (line 53)' cannot be converted to 'PersonEntity.person'. E:\Dropbox\Work Experimental\PersonEntity- Copy\PersonEntity\personRepository.vb 61 29 PersonEntity

I've gotten further with this code but I still cannot have the attached events as I get yet another error with the casting, this time of events. Generic List to ObservableList doesn't convert apparently. It seems an incredible way round to answer my original question!

Dim test = context.persons.Where(Function(r) r.personActive).Select(Function(o) New With { _
                 .person = o, _
                 .events = o.events.Where(Function(e) e.eventDateTime > startdate) _
            })


            Dim Res = New ObservableCollection(Of person)

            For Each t In test
                Dim r As New person
                r = t.person
                r.events = t.events
                Res.Add(r)
            Next

            Return Res
Ry-
  • 218,210
  • 55
  • 464
  • 476
Richard Griffiths
  • 758
  • 1
  • 11
  • 23

1 Answers1

1
Function(o) New With { _
         .person = o, _
         .events = o.events.Where(Function(e) e.eventDateTime > startdate) _
    }

This returns an anonymous type, not a Person. Did you mean to Add its person property*?

For Each o In test
    Res.Add(o.person)
Next
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • This was part of another problem, which I've just managed to resolve in another question. Thank you. And your right :) That's exactly what I had to do. – Richard Griffiths Dec 18 '12 at 00:00