Breeze - How to create contains Query - breeze

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

Related

How to combine a multiMatchQuery with a boolQuery using Spring Data Elastic Search

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

How to include FirstOrDefault() in ToList()

I want to get only one file for each recipe.
var UploadedFiles = (from rec in db.Recipes
join files in db.Files on rec.Id equals files.RecipeId
select new
{
files.Id,
files.Path,
files.RecipeId,
rec.Name,
rec.Description,
rec.Category,
rec.CookTime
}).ToList();
return new JsonResult { Data = UploadedFiles, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
You could use group join instead of regular join, I presume it is also more efficient than the previous answer (with the let), although I am not fully aware of EF query optimizations in this case
var UploadedFiles = (from rec in db.Recipes
join files in db.Files on rec.Id equals files.RecipeId into g
let firstFile = g.FirstOrDefault()
select new
{
firstFile.Id,
firstFile.Path,
firstFile.RecipeId,
rec.Name,
rec.Description,
rec.Category,
rec.CookTime
}).ToList();
Update
since I don't use EF, I can't really confirm whether or not it handles nulls but I have been informed it doesn't you would have to remove nulls.
var UploadedFiles = (from rec in db.Recipes
join files in db.Files on rec.Id equals files.RecipeId into g
let firstFile = g.FirstOrDefault()
where firstFile != null
select new
{
firstFile.Id,
firstFile.Path,
firstFile.RecipeId,
rec.Name,
rec.Description,
rec.Category,
rec.CookTime
}).ToList();
You can try the following...
var UploadedFiles = (from rec in db.Recipes
from files in db.Files.FirstOrDefault(f => f.RecipeId == rec.Id)
select new
{
files.Id,
files.Path,
files.RecipeId,
rec.Name,
rec.Description,
rec.Category,
rec.CookTime
}).ToList();
return new JsonResult { Data = UploadedFiles, JsonRequestBehavior =
JsonRequestBehavior.AllowGet };

breeze.js mixing AND and OR predicates

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

Returning property and count columns together in Neo4jClient Cypher Query

I have a Cypher query likes this:
START n=node:permit_idx(PmtID= "111")
Match n-[:Assinged]->m<-[:Assinged]-p
RETURN p.PmtID, count(m);
I got error when I try to do it using Neo4jClient Cypher Query
var results = graphClient
.Cypher
.Start(new { n = Node.ByIndexLookup("permit_idx", "PmtID", "111") })
.Match("Match n-[:Assigned]->m<-[:Assigned]-p")
.Return((m, p) => new
{
PDPmtID = "p.PmtID",
MCount = "count(m)"
})
.Results;
If only need to return one property or one count, we can use
.Return<int>("count(m)");
But how to return property and count together?
.Return((m, p) => new
{
PDPmtID = Return.As<int>("p.PmtID"),
MCount = m.Count()
})
Or, preferred right now:
.Return((m, p) => new
{
Permit = p.As<Permit>(),
MCount = m.Count()
})
You need to use the custom text option in your compound Return clause:
.Return((m, p) => new
{
PDPmtID = Return.As<int>("p.PmtID"),
MCount = Return.As<int>("count(m)")
})
(This is based on the documentation for the Neo4jClient)

Breeze implementation of 'AND' and 'OR' in the where method

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

Resources