?
Solved

Entity Framework and SQL Queries

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 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