Solved

Entity Framework and SQL Queries

Posted on 2010-11-17
4
469 Views
Last Modified: 2013-11-26
I recently read a book on VS2010. There was a chapter on the Entity Framework. It seems in the chapter it was showing an alternative method of querying data, not using LINQ, but building a SQL query string and passing that in somehow. I've since checked the book back into the library. Maybe I'm wrong.

Can someone please tell me how to use a SQL query with Entity Framework instead of LINQ?

Thanks!
0
Comment
Question by:Starr Duskk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Author Comment

by:Starr Duskk
ID: 34160499
I think I found how to do this:
http://msdn.microsoft.com/en-us/library/bb738521.aspx
Using context As New AdventureWorksEntities()
    ' Create a query that takes two parameters.
    Dim queryString As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contacts " & _
        " AS Contact WHERE Contact.LastName = @ln AND Contact.FirstName = @fn"

    Dim contactQuery As New ObjectQuery(Of Contact)(queryString, context)

    ' Add parameters to the collection.
    contactQuery.Parameters.Add(New ObjectParameter("ln", "Adams"))
    contactQuery.Parameters.Add(New ObjectParameter("fn", "Frances"))

    ' Iterate through the collection of Contact items.
    For Each result As Contact In contactQuery
        Console.WriteLine("Last Name: {0}; First Name: {1}", result.LastName, result.FirstName)
    Next
End Using

Can anyone confirm that I could indeed build a sql query string and pass it in using the ObjectQuery and I don't have to ever use Linq?

thanks.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 34161129
One problem with LINQ is that it can be very static, and not handle dynamic queries.  That example shows how you can get around that dynamic problem, and build your own query through the entity context.  If you don't need that dynamic approach, then I would stick with the LINQ example, since it handles the associations, and the query syntax generation.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 34161935
Many of my queries are huge, based on the search values selected on the search form. I do a lot of if this search value is selected, include this table on a JOIN and include this data, if that value is selected and so on for a lot of JOINs and where clauses. So yes, I can see using the Linq for the basic easy stuff, but for the detailed search queries, it's nice to still have that option.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 34164172
Search forms and LINQ are usually a perfect case for dynamic queries, since you usually have a lot of search options that are optional.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question