linQ to entities does not recognize the method Models.Repository.Student get_Item(Int32)' method - entity-framework

getting this error on where clause.
trying to fetch records from sql server with linQ query in entity framework.
var Stud = contextSchool.Student.Where(x => x.STDNT_ID == lstStudent[i].StudentID).FirstOrDefault();
if i store value of list in a variable and then use in where clause, it works but not with list.
complete error:
linQ to entities does not recognize the method Models.Repository.Student get_Item(Int32)' method, and this method cannot be translated into a store expression.

i also get this error on some other projects, the problem is that u cant use methods like get_Item or f.e. AddDays() in ur linq command.
So u kinda have to play around it.
U have to create an object (list, or whatever) before ur linq command and fill it with the data u need (per methods) and use the variable in ur linq command.

Related

LIKE Column query

It is easy to do a Contains, StartsWith, or EndsWith query in Entity Framework when you know the pattern you'd like to find. But how to do the equivalent of "LIKE [Column]"?
For instance, this is easy:
Name LIKE '%Th'
But how do you do this? where Prefix is a column.
Name LIKE [Prefix]
I tried to use the SqlMethods and got the following error.
LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression.
I resolved this problem by using LINQ to Entities to bring back the set of data that I wanted, then using LINQ to Objects to filter the data.
I do not think there is currently a way to have a LINQ to Entities query be resolved in a database server as ...
... LIKE [column-name]
First, SqlMethods is for use in LINQ-to-SQL, not LINQ-to-Entities.
The best you can do to use the filter in the database engine (so you don't have to pull all data in memory first) is:
context.Entities.SqlQuery("SELECT * FROM EntityTable WHERE Name LIKE Prefix")

Dynamic EF Query

I am thinking of designing a business rule engine which basically generates an EF query from a set of string values stored in a database.
For e.g. I will store the connection string, table name, the where condition predicate, and select predicate as string fields in a db and would like to construct the EF query dynamically. For e.g.
var db = new DbContext(“connectionstring”);
var wherePredicate = Expression.FromString(“p => p.StartDate > new DateTime(2014,5,1))
var selectPredicate = Expression.FromString(“p => p”)
var results = db.Set(“Projects”).Where(wherepredicate).Select(selectPredicate)
For constructing the predicates I can use DynamicExpression or Dynamic LINQ library.
However how do I access db.Set(“Projects”) where Projects is the entity name and apply the where and select predicates? (or something like db[“Projects”].Where().Select).
I tried the non-generic version of the DbContext.Set(Type entityttype) method, however couldn’t figure out how to apply Where and Select predicates to the returned object.
I am trying to avoid generating SQL queries and instead rely on dynamically generated EF code.
This doesn't make much sense. You can create method that will work on string instead of generic type using reflection, but you'd have to return DbSet not DBSet<T>. And on that one you cannot execute LINQ's methods (basically), because there's no type (during compilation). Of course you can do it all the way using reflection, but then, why??? You're loosing 90% of what O/R mapper does for you.

Getting SqlCommand with EF

I made a SqlDependency service in my application. It works perfectly when I type the queries by hand but I cannot include wildcards (I don't really know why).
For example:
//Using this SqlCommand will work
new SqlCommand("SELECT [employees].[name] FROM [dbo].[employees]", sqlNotificationConn)
//But this one won't
new SqlCommand("SELECT [employees].* FROM [dbo].[employees]", sqlNotificationConn)
//And this one won't either
new SqlCommand("SELECT * FROM [dbo].[employees]", sqlNotificationConn)
So basically, I want to get my DbContext to generate a full SELECT command with every fields it deals with.
In Linq 2 SQL, I used this service using dbContext.GetCommand(.....);
In EF 4.0 (or was it 4.1?), I used dbContext.employee.ToTraceString();
But in EF 4.4, I can't find anything to generate that SELECT query string....
With DbContext (DbQuery) it is as simple as:
query.ToString()
With ObjectContext (ObjectQuery):
((ObjectQuery)query).ToTraceString()
By the way, query can be any expression based on a DbSet (or ObjectSet, respectively). So something like dbContext.employee.Where(e => e.Name == "Gates").ToString() will also show the generated SQL query.
A LINQ statement that forces execution, like ToList(), Single(), FirstOrDefault(), etc, creates a new object and ToString() will return the object's type name.
ToTraceString() is still in EF, same place it always was. However, it's on ObjectQuery, not DbQuery. Having said that, I've never seen the SQL Server provider for EF use *; it always uses discrete fields in SQL.

Entity SQL: Include?

I am looking for equivalent of the following for Entity SQL.
from row in Parent_Table
select new{
Parent_Table.include("child_table").Select("SomeField").Max(),
row
}
Whereas this is part of whole query
(how can i create a sub query while the lazy loading is off ?)
as i try
myObjectQuery.Include("Proj_PF_" + state_Year).Select("phisicalImprovement").Max();
phisicalImprovement is a field of Proj_PF_" + state_Year and my query is to an other table
get the fallowing exception
'phisicalImprovement' is not a member of 'Transient.collection[NezaratModel.Proj_PF_58_89(Nullable=True,DefaultValue=)]'. To extract a property of a collection element, use a subquery to iterate over the collection. Near simple identifier
Thank you.
There is no equivalent in ESQL and it should not be needed because when you call ESQL you are using ObjectQuery<> so you just pass your ESQL command to the ObjectQuery instance and should be able to call Include for it:
var query = (new ObjectQuery<YourEntity>("SELECT VALUE ...")).Include("SomerRelation");

Call function in query in Entity framework 3.5

I am trying to run following query in entity framework 3.5
var test = from e in customers where IsValid(e) select e;
Here IsValid function takes current customer and validate against some conditions and returns false or true. But when I am trying to run the query it is giving error "LINQ Method cannot be translated into a store expression." Can any body tell me any other approach?
One approach I can think of is to write all validation conditions here, but that will make the code difficult to read.
Thanks
Ashwani
As the error text implies, the EF Linq provider can not translate the IsValid method into SQL.
If you want the query to execcute in the DB, then you would have to write your validation conditions in the linq statement.
Or if you simply want to fetch all customers and then check if they are valid you could do:
var test = from e in customers.ToList() //execute the query directly w/o conditions
where IsValid(e)
select(e);