Print Page | Close Window

How do I get the results of query into observable collection?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2630
Printed Date: 29-Jul-2026 at 8:30pm


Topic: How do I get the results of query into observable collection?
Posted By: Terry Wells
Subject: How do I get the results of query into observable collection?
Date Posted: 21-Apr-2011 at 5:26am
Hello All,
 
Really simple I'm sure but I'm trying to get my query into an observable collection within a VB WinForms app?
 
 Dim mgr As New OperaEntities()
 Dim myCompanyHistory As New ObservableCollection(Of tblCompanyHistory)
 Dim qry = From comphist In mgr.tblCompanyHistories Where comphist.CREF = 11414
           Select comphist
               
     mgr.ExecuteQuery(qry) <---I can't figure out the syntax here to push the results into the MyCompanyHistory collection
     Me.RadGridView1.DataSource = myCompanyHistory
 
Any pointers? Is this the correct methodolgy to use?
 
Many thanks
 
Terry
 



Replies:
Posted By: smi-mark
Date Posted: 21-Apr-2011 at 8:16am
Hi Terry,

You can do it a few ways, here are a couple examples:

Dim mgr As New OperaEntities()
Dim myCompanyHistory As ObservableCollection(Of tblCompanyHistory)
Dim qry = From comphist In mgr.tblCompanyHistories Where comphist.CREF = 11414 Select comphist               
dim results = mgr.ExecuteQuery(qry)

myCompanyHistory  = new ObservableCollection(Of tblCompanyHistory)(results)
Me.RadGridView1.DataSource = myCompanyHistory

Dim mgr As New OperaEntities()
Dim myCompanyHistory As new ObservableCollection(Of tblCompanyHistory)
Dim qry = From comphist In mgr.tblCompanyHistories Where comphist.CREF = 11414 Select comphist               
dim results = mgr.ExecuteQuery(qry)

results.ForEach(function (r) myCompanyHistory.Add(r))  <-- I think this is the VB.Net syntax for in. in C# we simply do results.ForEach(myCompanyHistory.Add);
Me.RadGridView1.DataSource = myCompanyHistory


Posted By: Terry Wells
Date Posted: 21-Apr-2011 at 8:48am
Thanks for that,
 
Your first option doesn't quite work I get "Overload resoltion failed because no accessible 'New' can be called without a narrowing converision" on line myCompanyHistory = New ObservableCollection(Of tblCompanyHistory)(results)
 
However your second option gave me the answer I was looking for although
results.ForEach(function (r) myCompanyHistory.Add(r)) doesnt work,
 
but I did it the long way round with
 
For Each r In results
        myCompanyHistory.Add(r)
Next
 
Thanks again Mark
 
Regards
 
Terry
 



Print Page | Close Window