Answered by:
How to run a query statement using SqlQuery and DbContext

Question
-
hi all
i am trying to run a simple statement which returns me a date (String) but i cant get the proper syntaxe :
private static string GetMinUpgradeDate() { UpgradeDataContext db = new UpgradeDataContext(); //return db.Database.SqlQuery(typeof(string), "select min(UpgradeDate) from dbo.NightlyTargets"); //return db.Database.SqlQuery<string>("select min(UpgradeDate) from dbo.NightlyTargets"); //Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string' }
any ideas?
thank you
Max- Edited by Maximusdm Thursday, October 27, 2011 3:15 AM
Thursday, October 27, 2011 3:15 AM
Answers
-
Hi Max,
using (var context = new UnicornsContext()) { var unicornNames = context.Database.SqlQuery<string>( "select Name from Unicorns").ToList(); }
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Maximusdm Friday, October 28, 2011 3:32 AM
Friday, October 28, 2011 2:35 AM -
Thanks for replying but the fix was to use
FirstOrDefault()
thanks for replying!
Max- Marked as answer by Maximusdm Friday, October 28, 2011 3:32 AM
Friday, October 28, 2011 3:25 AM
All replies
-
Hi Max,
using (var context = new UnicornsContext()) { var unicornNames = context.Database.SqlQuery<string>( "select Name from Unicorns").ToList(); }
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Maximusdm Friday, October 28, 2011 3:32 AM
Friday, October 28, 2011 2:35 AM -
Thanks for replying but the fix was to use
FirstOrDefault()
thanks for replying!
Max- Marked as answer by Maximusdm Friday, October 28, 2011 3:32 AM
Friday, October 28, 2011 3:25 AM