How can I get linqpad to understand CreateSourceQuery in the following C# statements?
var airline = Airlines.FirstOrDefault(a => a.ID == 1776);
airline.Dump();
var crew = airline.Crew.CreateSourceQuery().Where(c => c.Title == "pilot");
crew.Dump();
Where Airlines.Crew is a navigation property. Linqpad gives the error:
'System.Data.Linq.EntitySet' does not contain a
definition for 'CreateSourceQuery' and no extension method
'CreateSourceQuery' accepting a first argument of type
'System.Data.Linq.EntitySet' could be found
Is this perhaps just a reference I need?
CreateSourceQuery is an Entity Framework method. LINQPad uses LINQ-2-SQL by default.
There's a walk-through on the LINQPad website which explains how to use it with Entity Framework:
http://www.linqpad.net/EntityFramework.aspx
Related
This works:
string sql = $"SELECT * FROM {tableName}";
var executeSQL = db.Set<MyTableContextType>().FromSqlRaw(sql).ToList();
But this code will be accepting requests for multiple tables (past MyTableContextType). Rather than hardcoding MyTableContextType, I'd like to do something like this:
dynamic entityType = db.Model.FindEntityType(stringMyType);
string sql = $"SELECT * FROM {tableName}";
var executeSQL = db.Set<entityType>().FromSqlRaw(sql).ToList();
All of the above works, except the last line which gives this error:
'entityType' is a variable but is used like a type.
How can a variable be fed into db.Set<>?
If there is a better way, I am open to that too.
.NET Core 3.1
& EF Core 3.1
It won't work since Generics is a compile-time concern, but type from FindEntityType is a runtime concern.
In your case, extracting db.Set<MyTableContextType>().FromSqlRaw(sql).ToList() to generic method may help.
Something like
private TEntity SelectAll<TEntity>(string tableName) => db.Set<TEntity>().FromSqlRaw($"SELECT * FROM {tableName}").ToList();
Alternative way to solve it - use LINQ to SQL methods instead of raw SQL in order to select the data.
What are some naming conventions utilized for Entity Framework queries?
Example, following code utilizes 'e. Why do they use e? And what are naming convention strategies for delegate abbreviation in method syntax? This question is not asking for opinion, just common naming convention.
public static string GetBooksByPrice(BookShopContext context)
{
var result = context.Books
.Where(e => e.Price > 40)
.OrderByDescending(e => e.Price)
.Select(e => new
{
e.Title,
e.Price
}).ToList();
You can name it to book as well rather than e, you can follow the same naming conventions other than reserved C# Keywords
It has nothing to do with Entity Framework you're using C# lambda expressions to query the database which in turn is translated to SQL server statement
You can read about Lambda expressions here
C# Lambda Expressions
There is no suggested naming convention for delegate. If you see the linq at github Select. These are method parameters so you can pass any name but type need to be same.
In “Programming Entity Framework”, 2nd edition by Julia Lerman, in chapter dedicated to Entity SQL, p. 115, we have the following example of using query builder method to do projection using EF 4.1 .Where and .Select with string parameters:
ObjectQuery<DbDataRecord> contacts = context.Contacts
.Where("it.FirstName='Robert'")
.Select("it.Title, it.FirstName, it.LastName");
I am using Entity Framework 6, .Net 4.6, VS 2015. The compiler complains that there are no .Where and .Select that accept string parameters, only lambdas. Is there any solution how to follow this book example?
The example appears to be about the old ObjectQuery API, which shouldn't really be used now. It's still possible to use it with EF6.x though, with something like the following:
ObjectContext objectContext = ((IObjectContextAdapter)conte).ObjectContext;
ObjectSet<DbDataRecord> objectSet = objectContext.CreateObjectSet<DbDataRecord>("DbDataRecords");
// Async version: var res0 = await objectSet.Where("it.FirstName='Robert'").ToListAsync();
var res0 = objectSet.Where("it.FirstName='Robert'").ToList();
That said, you really should use the lambda instead with the new DbContext API.
I can successfully return a model from my controller like this:
return View(lemonadedb.Messages.ToList() );
It's interpreted perfectly by my view.
Now I only want to show the messages where Messages.user == Membership.GetUser().ToString().
But when I do this:
return View(lemonadedb.Messages.Where( p => p.user == Membership.GetUser().ToString()).ToList());
I get:
'LINQ to Entities does not recognize
the method 'System.String ToString()'
method, and this method cannot be
translated into a store expression.'
I need some way to narrow down the results of the messages table.
Should I use the find() method somehow? I thought it was only for ID's.
How should I do this?
The reason you're having this issue is that Entity Framework is trying to evaluate the expression Membership.GetUser().ToString() into an SQL query. You need to create a new variable to store the value of this expression and pass it into your query. Entity Framework will then just interpret this as you expect.
The following should work:
var user = Membership.GetUser().ToString();
return View(lemonadedb.Messages.Where(p => p.user == user).ToList());
I suspect this is a very common mistake that people make when writing Entity Framework queries.
Is there any good and, if possible, exhaustive documentation about ESQL in entity framework ?
I'm trying to make a select of an entity object with modification of a property using a method; something like this :
SELECT foo FROM context.foo WHERE foo.Price = AddTaxes(foo.Price)
There is msdn providing some documentation Entity SQL Language
You can also combine it with Linq2Entities with something like
context.foo
.Where("it.Price == #Price", new ObjectParameter[]
{ new ObjectParameter("Price", AddTaxes(price) } ).ToList()