I'm trying to configure the proper syntax for breeze.js when mixing AND and OR predicates. I did not see an example on the breeze site and could not find one anywhere else.
Basically I want to do something like this in my WHERE clause:
(
age > 30 AND
sex == 'M' AND
jobStartDate >= '1/1/2000'
)
OR
(
exemptStatus == 1
)
this will bring back entities that match the 3 criteria OR are exempt. I'm using EF6 with ODATA syntax.
Thanks
Try this:
var Predicate = breeze.Predicate;
var baseQuery = EntityQuery.from("Something");
var pred1 = new Predicate("age", ">", 30);
var pred2 = new Predicate("sex", "==", 'M');
var pred3 = new Predicate("jobStartDate", ">=", new Date(2000,1,1));
var pred4 = new Predicate("exemptStatus", "==", 1);
var compositePred = pred1.and(pred2).and(pred3).or(pred4);
var query = baseQuery.where(compositePredicate);
myEntityManager.executeQuery(query);
or
var compositePred = Predicate.and(pred1, pred2, pred3).or(pred4);
Related
I am using a multi match query like so:
var builder = QueryBuilders.multiMatchQuery(search, "projectName", "jobNumber");
var query = new NativeSearchQueryBuilder()
.withQuery(builder)
.withPageable(pageable)
.build();
var result = searchOperations.search(query, Foo.class);
Now I need to make sure that the _id field also matches the FooId field. How do I create a query to do that and combine it with my multi match query?
If both clauses are mandatory, you can combine them using must method of BoolQueryBuilder:
var builder = new BoolQueryBuilder()
.must(QueryBuilders.multiMatchQuery(search, "projectName", "jobNumber"))
.must(QueryBuilders.termQuery("_id", fooId));
var query = new NativeSearchQueryBuilder()
.withQuery(builder)
.withPageable(pageable)
.build();
var result = searchOperations.search(query, Foo.class);
Or you can define the clause by id as a filter having the same result:
var builder = QueryBuilders.multiMatchQuery(search, "projectName", "jobNumber");
var filter = QueryBuilders.termQuery("_id", fooId)
var query = new NativeSearchQueryBuilder()
.withQuery(builder)
.withFilter(filter)
.withPageable(pageable)
.build();
var result = searchOperations.search(query, Foo.class);
I'm working on an ASP.NET MVC application with Entity Framework 6 and a SQL Server database.
I'm trying to shuffle the results of a query by adding a SortingCode which I'd like to assign a value based on the current SessionId, so that every time the returned rows are shuffled without affecting the pagination. SortingCode in this attempt is a string, but it can be any type, as long as it allows me to get shuffled results. I have something like this:
var sessionId = Session.SessionID.GetHashCode();
var rnd = new Random(sessionId);
var query = (from l in _context.Adverts
select new AdvertSummary
{
Id = l.Id,
Title = l.Title,
Description = l.Description,
SortingCode = l.Title.OrderBy(x => rnd.Next()).ToString(),
});
The IQueryable result is then converted into a list later on in my code with:
var pagedResults = query.Skip(skip).Take(pageSize).ToList();
The above attempt with the Random class doesn't work, and results in an error
DbExpressionBinding requires an input expression with a collection ResultType
Is there anything that I can do to get shuffled results?
I would suggest to use SqlFunctions.Checksum for such task. SortingCode will be nearly close to the seeded Random.
var sessionId = Session.SessionID;
var query =
from l in _context.Adverts
select new AdvertSummary
{
Id = l.Id,
Title = l.Title,
Description = l.Description,
SortingCode = SqlFunctions.Checksum(sessionId, l.Title)
};
var pagedResults = query
.OrderBy(x => x.SortingCode)
.ThenBy(x => x.Id)
.Skip(skip)
.Take(pageSize)
.ToList();
How can I query in breeze a contains query of list if integers?
kind of this?
elemnts.where(p => listofids.contains(p.id));
For now you need to use 'or' clauses
var whereClause = breeze.Predicate.create("id", "==", 3)
.or("id", "==", 5)
.or("id", "==", 6);
var query = breeze.EntityQuery.from("elemnts").where(whereClause);
or ( with your list of ids):
var preds = listOfIds.map(function(id) {
return breeze.Predicate.create("id", "==", id);
})
var whereClause = Predicate.or(preds);
var query = breeze.EntityQuery.from("elemnts").where(whereClause);
My controller implicitly creates var x as so:
var x = from sem in db.Semesters
join ss in db.StudentSemesters on sem.Id equals ss.SemesterId into sss
from ss in sss.Where(ss => ss.StudentId == id).DefaultIfEmpty()
select new
{
SemsterName = sem.Name,
SemesterId = sem.Id,
IsAssociated = ss.StudentId != null
};
Instead of passing this data to the view as is, I want to pass it in as a strongly typed businessmodel called StudentSemesterAssociation (members: string SemesterName, int SemesterId, bool StudentIsAssociated) which is used in a viewmodel.
How do I adapt the var generating query above so as to return StudentSemesterAssociation?
ViewModel.StudentSemesterAssociation = ???
regards, Guy
Add your class name after the new keyword in the select part of your query:
var x = from sem in db.Semesters
join ss in db.StudentSemesters on sem.Id equals ss.SemesterId into sss
from ss in sss.Where(ss => ss.StudentId == id).DefaultIfEmpty()
select new StudentSemesterAssociation
{
SemsterName = sem.Name,
SemesterId = sem.Id,
IsAssociated = ss.StudentId != null
};
This will make x type IEnumerable<StudentSemesterAssociation> instead of a collection of anonymous types.
And btw. var does not make your variable weakly typed. You should read about the var keyword and implicitly typed local variables.
Breeze can query for condition1 AND condition2 by using multiple where statements. But can it query for condition1 OR condition2?
If not, what is the recommended way of achieving the same effect when using Breeze?
I am currently thinking the only way to do this is to perform the query on the server with a special action method, which creates some problems.
Use the Predicate like this sample:
var p1 = new breeze.Predicate("IsArchived", "==", false);
var p2 = breeze.Predicate("IsDone", "==", false);
var predicate = p1.and(p2);
var query = new EntityQuery("Todos").where(predicate);
Documentation is available here:
http://www.breezejs.com/documentation/query-filter
OK, the problem is the Breeze API and Docs are a little lacking with some typos.
The answer is to use the 'or' method of the Predicate class to create a new Predicate object that is passed to the where method.
Given 3 predicates you 'or' them together like so:
var newPred = p1.or(p2, p3);
or
var preds = [p2, p3];
var newPred = p1.or(preds);
or fluent method:
var p4 = Predicate.create("ShipCity", "startswith", "F")
.or("Size", "gt", 2000);
or using the static 'or' method
var newPred = Predicate.or(p1, p2, p3);