Solved

Entity Framework and SQL Queries

Posted on 2010-11-17
4
467 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:BobCSD
  • 2
  • 2
4 Comments
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
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
Comment Utility
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 1

Author Comment

by:BobCSD
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now