[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Entity Framework and SQL Queries

Posted on 2010-11-17
4
Medium Priority
?
473 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
  • 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 2000 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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

834 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