6

I am using an Entity Framework Database first model and I am having problems querying my DB.

I'm trying to use the following code:

var ctx = new dbEntities();
var description = ctx.DEPARTMENTs.SqlQuery("SELECT DESCRIPTION FROM DEPARTMENT WHERE DEPARTMENT='FINANCE'");
System.Diagnostics.Debug.WriteLine(description);

I can see my table of results for the DEPARTMENTs table in the debug log, but for some reason the SQL query is just returning me this in the console rather than executing the query, anybody know why?

SELECT DESCRIPTION FROM DEPARTMENT WHERE DEPARTMENT='FINANCE'
2
  • You're basically doing description.ToString(), which returns the string representation of the query. Commented Feb 26, 2014 at 9:57
  • If someone's answer solved your problem, you might want to accept it as the answer. Commented Feb 26, 2014 at 10:09

3 Answers 3

8

To make the query execute against the provider, you have to use any extension derived from IEnumerable i.e. ToList() or First() or FirstOrDefault()

Now you have to consider few things. What your query might possibly return? A List of data or a single data? Or even if a list of matches found, you want just the single one?

from the syntax of your query I assume you should be doing this:

var ctx = new dbEntities();
var dep = ctx.DEPARTMENTs
             .SqlQuery("SELECT * FROM DEPARTMENT WHERE DEPARTMENT='FINANCE'")
             .FirstOrDefault();

if(dep!=null)
{
    var description = department.Description;
}

Alternatively, you can also do this(I would prefer):

var description = ctx.DEPARTMENTs.Where(s=>s.Department.ToUpper() =="FINANCE")
                     .Select(s=>s.Description)
                     .FirstOrDefault();
Sign up to request clarification or add additional context in comments.

2 Comments

This is good - I like this answer, how can I choose extra parameters in the select statement though, say I want to choose Description and ID
ohh, very simple, do this .Select(s=>new{ s.Description, s.ID, s.WhateverElseAvailable})
4

Try:

var description = ctx.DEPARTMENTs.SqlQuery<string>("SELECT DESCRIPTION FROM DEPARTMENT WHERE DEPARTMENT='FINANCE'").ToList();

EDIT: Raw SQL Queries http://msdn.microsoft.com/en-us/data/jj592907.aspx

Note that, just as for LINQ queries, the query is not executed until the results are enumerated—in the example above this is done with the call to ToList.

System.Diagnostics.Debug.WriteLine(Object) writes the value of the object's ToString method to the trace listeners in the Listeners collection. Debug.WriteLine(Object)

1 Comment

Thanks for your response - I am getting the following error though : "The non generic method '...' .SQLQuery(string, params object[]) cannot be used with type arguments
1

SqlQuery returns DbSqlQuery<TEntity> object. In your case it is probably DbSqlQuery<DEPARTMENTs>. As the documentation says

Represents a SQL query for entities that is created from a System.Data.Entity.DbContext and is executed using the connection from that context. Instances of this class are obtained from the System.Data.Entity.DbSet instance for the entity type. The query is not executed when this object is created; it is executed each time it is enumerated, for example by using foreach. SQL queries for non-entities are created using the System.Data.Entity.DbContext.Database. See System.Data.Entity.Infrastructure.DbSqlQuery for a non-generic version of this class.

Please note the lines

The query is not executed when this object is created; it is executed each time it is enumerated, for example by using foreach.

Which means you have to iterate it to get the results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.