[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Entity Framework and SQL Queries

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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