Entity SQL: Include? - entity-framework

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");

Related

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

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.

Spring Data Jpa equality of two column

I want to learn if we could write a query that has a condition like
List<Entity> findbyField1EqualsField2();
This method should not take any parameter . It should fetch entities which has a field1 equals field2. It is just a simple sql :
select * from entity where field1=field2.
But I could not find any solution yet. Thanks.
Create an operation with a query as next:
#Query("select t from entity t where t.field1 like t.field2")
List<T> findByField1LikeFie‌ld2();
I don't think findByField1LikeFie‌​ld2()works.... you would need to do it passing a param findByField1Like(St‌​ring param) and for this maybe you would need to load the entity before to get the value of field2.

Why Linq to Sql makes SELECT * instead of SELECT COUNT(*) when I need only Count?

We use Entity Framework and this leads to big performance problems whenever we use Count() on child collections of database entities.
As workaround I've used joins of root collections of data context. Then the resulting sql query uses the desired COUNT(*). But this solution is really ugly.
The slow query is:
var booked = erf.Sessions.All(s => s.Exams.All(e => e.Candidates.Count() >= e.CandidatesExpected))
If by "child collections" you mean navigation properties of type ICollection<T> defined in your entities, then it's Linq-to-Entities (not Linq-to-Sql, as you specified). Thus your Count() method is just an extension method defined in Enumerable class, which is executed on entities already materialized into memory. To get results you are expecting, you need to use Count() on DbSet queries.

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.

Doubt regarding JPA namedquery

I am trying to execute a namedquery
#NamedQuery(name="getEmployeeDetails",query="select e.username,e.email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1")
Now when I execute this query in a EJB 3.0 Session Bean what is the object I should return.I tried returning Listits returning a Vector which creates a classcast exception.The employee table contains fields like password and other confidential details which I don't want to fetch.So I am not using select e from Employee e.
I am learning JPA can anyone help.
Below is the sample query which fetches only the required fields, but have to make such constructor for it.
Query : SELECT NEW package_name.Employee(e.username,e.email,e.image,e.firstname,e.lastname) FROM Employee e where e.empid=?1;
It will return Employee entity with selected fields & remaining will have default values.
Inspect the returned type by calling .getClass() on a returned object. I'd guess it's an array.
But this is not really a good way to use JPA. Select the whole entity and then just don't use what you don't need. It's not such a performance hit.