Chris Kingsley
Chris Kingsley

Reputation: 3

Linq query returns corresponding sql when executed

I have created a database. When I instantiate the data context and run the query (for viewing purpose I attached a textblock, so the result is displayed in textblock) I get the SQL from of the query instead of the result.

My code:

using(LanguagesContext ctx = new LanguagesContext(LanguagesContext.ConnectionString))
{
   ctx.CreateIfNotExists();
   ctx.LogDebug = true;

   var abc = from p in ctx.SentencesDB
   where p.English == "hello"
   select p.English;

   return abc.ToString();
}

This is the output I get :

SELECT [t0].[English]
FROM [SentencesDB] AS [t0]
WHERE [t0].[English] = @p0

So, where have I gone wrong?

Upvotes: 0

Views: 68

Answers (2)

Marek Woźniak
Marek Woźniak

Reputation: 1776

or select other query:

string abc = (from p in ctx.SentencesDb
          where p.English == "hello"
          select p.English).SingleOrDefault("none")
return abc;

second way:

string abc = ctx.SentenceDb
             .SingleOrDefault(p => p.English == "Hello")
             .Select(p => p.English);

Upvotes: 0

artm
artm

Reputation: 8584

You are getting SQL command becuase you're doing a ToString on the linq query, whose ToString is what you get. If you want to get the results you need to return abc and do a foreach on it.

Upvotes: 1

Related Questions