I have OData Web API + EF app and I need to modify some query in nested where. if a query looks like:
SomeEntity?$filter=Name eq 'Y'&$Expand=Children($filter=Name eq 'x')
It nests LINQ expression in a way that I can't just do
IQueryable.Where(y => additional filter) which only applies to SomeEntity and not Children entity.
I have ExpressionVisitor that returns modified Expression for individual Where LINQ func but IQueryable.Expression is read-only and I am unable to overwrite this new to old Expression.
I found that i can do
IQueryable.Provider.CreateQuery(my new expression)
Related
I'm currently working with ASP.NET Core and I want to use Automapper to map a Linq Expression. The mapping statement is:
var targetConditions = _mapper.Map<Expression<Func<Entity, bool>>>(filter);
where filter is a formal parameter in the form:
(Expression<Func<EntityDTO, bool>> filter
In the mapping profile I have the following map created:
CreateMap<Expression<Func<EntityDTO, bool>>, Expression<Func<Entity, bool>>>();
I'm using a generic repository pattern with EF. I want to get a list of DTOs filtered by, of course, DTO's fields from my controller. I then need to convert from DTO filter to entities filter in the Business Layer before doing any query using Linq for EF.
Even though the expression does get coverted from EntityDTO to Entity, the parameters in the lambda expressions inside don't, raising all sorts of errors when I further use it with EF. Any idea how can this be done?
Try this:
IQueryable<Customer> query = repository.getCustomers(); // entities query
query.ProjectTo<CustomerDto>().Where(dtoFilterExpression)
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> findByField1LikeField2();
I don't think findByField1LikeField2()works.... you would need to do it passing a param findByField1Like(String param) and for this maybe you would need to load the entity before to get the value of field2.
How can I achieve following query method in Entity Framework,
below is a snippet from NHibernate documentation http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html
Example example = Example.create(cat)
.excludeZeroes() //exclude zero valued properties
.excludeProperty("color") //exclude the property named "color"
.ignoreCase() //perform case insensitive string comparisons
.enableLike(); //use like for string comparisons
List results = session.createCriteria(Cat.class)
.add(example)
.list();
Entity framework is LINQ based. Linq is said to be a declarative language, which means so much as telling what to do in stead of how to do it (imperative). A statement like
context.Orders.Select(o => o.OrderDate).Distinct();
is a declarative shortcut, if you like, for a 'ceremonial' foreach statement in which OrderDates are added to a list if they were not added to it before.
I'm not an expert in NHibernate or its criteria API, but the criteria API seems to be even more declarative than linq. That makes it hard to compare them. A few differences:
The main one: query by example is not possible in EF.
There is no way in linq to set behaviours for a whole query. For instance, if you want to exclude zero valued properties, you'll have to specify each one of them in a where predicate (which is closer to telling how to filter).
Case sensitivity is downright underdeveloped in EF. For example, a statement like
People.Where(c => string.Compare( c.Name, "z", false) > 0)
will generate the same SQL as
People.Where(c => string.Compare( c.Name, "z", true) > 0)
The database collation determines the case sensitiveness of string comparisons.
You can do LIKE queries, but, again, specified for each individual predicate:
People.Where (c => c.Name.Contains("a"))
(again: no differentiation in case)
So I can't really give a linq translation of your criteria query. I'd have to know the class properties to be able to specify all individual predicates.
When using traditional Entity Framework and querying with ESQL, you can use OFTYPE with ONLY to only return base types.
See: http://msdn.microsoft.com/en-us/library/bb399295.aspx
In Entity Framework Code First, I have inheritance set up where B is a subtype of A. Performing MyContext.Set<A>().OfType<A>() still returns elements of type B. Ideally, I would like to call MyContext.Set<A>().OfOnlyType<A>() and it would translate the same way as when using ESQL's OFTYPE ONLY.
I also found I can use the is operator in a where statement, but again, that will return both A and B entities.
How can I write a linq expression that will filter to only elements of type A?
There is no way to get only A instances directly (equivalent to ONLY) in Linq-to-entities unless you somehow exclude derived instances from the result set. For example:
var list = MyContext.Set<A>().Where(a => !(a is B)).ToList();
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");