wrapping entity framework queries in class methods

A

Andy B

I am trying to use an ObjectDataSource with a GridView. I have created a
class called NewsManager that the ObjectDataSource will use for its data. I
am kind of confused on what the return type of the methods should be. This
is what I have so far:

Imports System.Data.Objects

Imports System.Data.Objects.DataClasses

Public Class NewsManager

Private DBContext As New eternityrecordsonlineEntities()

'*** Return the entire list of news articles.

Public Function GetAllNews() As News

Dim NewsQuery As ObjectQuery(Of News) = _

DBContext.News

Return NewsQuery

End Function

End Class



I get this error:



Error 1 Value of type 'System.Data.Objects.ObjectQuery(Of
WebApplication1.News)' cannot be converted to 'WebApplication1.News'.
C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\WebApplication1\WebApplication1\NewsManager.vb 10 10
WebApplication1


How should I fix this?
 
G

Gregory A. Beamer

Try List(Of T) and return a list of objects.

If you are only getting a single news record, you will have to pull it from
the list, or ObjectQuery, or whatever you desire to use to grab the records.

Am I hitting on track, or did I miss something?

--
Gregory A. Beamer
MVP: MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think Outside the Box! |
********************************************
 
A

Andy B

Hi.

You weren't missing anything. I should have thought to return the object
type that created the query. ObjectQuery(Of News). The final working version
is this:

imports System.Data.Objects
imports System.Data.Objects.DataClasses

public class NewsManager

'*** Create the entity context
private DBContext as new eternityrecordsonlineEntities()

'*** Get the entire list of news articles sorted by title.
public function GetAllNews() as ObjectQuery(Of News)
dim NewsQuery as New ObjectQuery(Of News) = _
DBContext.News.OrderBy("it.Title") '*** Haven't figured out how to order ASC
or DESC yet. Any ideas?
Return NewsQuery
End function
End class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top