Breeze version: 1.4.14
I have 2 entities: Person and Patient. Person has a list of Telephones and Patient inherit from Person.
I can query on the server:
var patients = Patients.Where(x => x.Telephones.Where(xx => xx.Number == "XXXX-XXXX").Any());
I can also get the results pasting this url on the browser:
/breeze/Patients?$filter=Telephones/any(x1: x1/Number eq 'XXXX-XXXX')
But if I try to execute a breeze query from the client
var query = breeze.EntityQuery.from("Patients");
query = query.where("telephones", "any", "number", "==", "XXXX-XXXX");
I get an error:
"TypeError: Cannot read property 'isAnonymous' of undefined breeze.debug.js:14807:34"
If I change the query to the base table "Person" it works:
var query = breeze.EntityQuery.from("Person");
query = query.where("telephones", "any", "number", "==", "XXXX-XXXX");
But I really need all the patient data... am I doing something wrong or is this a bug?
I do not now if this is a bug but I resolve it by calling toType() on my query:
var query = breeze.EntityQuery.from("Patients").toType("Patient");
query = query.where("telephones", "any", "number", "==", "XXXX-XXXX");
Hi use this code to solve your problem...
var query = breeze.EntityQuery.from("Patients);
query = query.where("telephones", "breeze.FilterQueryOp.any", "number", "breeze.FilterQueryOp.eq", "XXXX-XXXX");
Related
I am trying to get the latest date based on my controller below but I was hit with this error :
"Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType201[System.Nullable`1[System.DateTime]]]' to type 'System.IConvertible'."
var latestDt = from n in db.Books
where n.id == id
select new { Date = n.dtBookBorrowed};
DateTime dtPlus1Year = Convert.ToDateTime(latestDt);
May I know how do I get just the column latestDate in linq?
You can try this to get list of date order by latest insert to db.
var latestDt = db.Books.Where(n => n.id == id).OrderByDescending(x => x.dtBookBorrowed).Select(x => x.dtBookBorrowed).ToList();
I think if you use
DateTime.Parse(item.dateAsString)
your problem should be solved.
The LINQ query expression you've defined returns a collection of anonymous object with property Date despite there might be only one record match as ID was meant to be unique.
In your case we only need the target field that can be parsed as DateTime and therefore an alternative in fluent syntax would be as following:-
var book = db.Books.SingleOrDefault(book => book.id == id); // gets matching book otherwise null
if (book != null)
{
var borrowedDate = Convert.ToDateTime(book.dtBookBorrowed);
}
Otherwise if you would like to understand more about the behaviour with query syntax which may return multiple results, you may simplify as following which returns collection of DateTime object (i.e. IEnumerable) instead:-
IEnumerable<DateTime> borrowedDates =
from n in db.Books
where n.id == id
select Convert.ToDateTime(n.dtBookBorrowed);
I did raw SQL query below to select only certain fields from a table.
{
List<CustEmpVM> CustomerVMlist = new List<CustEmpVM>();
var cid = db.Customers.SqlQuery("select SchedDate from Customer where CustID = '#id'").ToList<Customer>();
}
But i keep getting the error of:
System.Data.Entity.Core.EntityCommandExecutionException occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The data reader is incompatible with the specified ALFHomeMovers.Customer. A member of the type, CustID, does not have a corresponding column in the data reader with the same name.
The exception message is pretty straightforward: the query expected to return full entity of Customer table but only SchedDate column returned, hence EF cannot done mapping other omitted columns including CustID.
Assuming Customers is a DbSet<Customer>, try return all fields from Customer instead:
// don't forget to include SqlParameter
var cid = db.Customers.SqlQuery("SELECT * FROM Customer WHERE CustID = #id",
new SqlParameter("id", "[customer_id]")).ToList();
If you want just returning SchedDate column, materialize query results and use Select afterwards:
var cid = db.Customers.SqlQuery("SELECT * FROM Customer WHERE CustID = #id",
new SqlParameter("id", "[customer_id]"))
.AsEnumerable().Select(x => x.SchedDate).ToList();
NB: I think you can construct LINQ based from the SELECT query above:
var cid = (from c in db.Customers
where c.CustID == "[customer_id]"
select c.SchedDate).ToList();
Similar issue:
The data reader is incompatible with the specified Entity Framework
Use below query instead of raw query:
{
List<CustEmpVM> CustomerVMlist = new List<CustEmpVM>();
var cid = db.Customers.Where(w=>w.Id == YOURCUSTOMERID).Select(s=>new Customer{SchedDate = s.SchedDate }).ToList();
}
It will give compile time error rather than run time error.
I am trying to get a path from a base node to its root node as 1 row. The Cypher query looks like this:
start n = node:node_auto_index(Name = "user1") match path = (n-[r:IS_MEMBER_OF_GROUP*]->b) return last(collect(distinct path));
But when changing this over to the Neo4JClient syntax:
var k = clientConnection.Cypher
.Start(new { n = "node:node_auto_index(Name = 'user1')" })
.Match("path = (n-[r:IS_MEMBER_OF_GROUP*]->b)")
.ReturnDistinct<Node<Principles>>("last(collect(path))").Results;
It gets an error:
{"Value cannot be null.\r\nParameter name: uriString"}
When continuing on from there:
Neo4jClient encountered an exception while deserializing the response from the server. This is likely a bug in Neo4jClient.
Please open an issue at https://bitbucket.org/Readify/neo4jclient/issues/new
To get a reply, and track your issue, ensure you are logged in on BitBucket before submitting.
Include the full text of this exception, including this message, the stack trace, and all of the inner exception details.
Include the full type definition of Neo4jClient.Node`1[[IQS_Neo4j_TestGraph.Nodes.Principles, IQS Neo4j TestGraph, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
Include this raw JSON, with any sensitive values replaced with non-sensitive equivalents:
{
"columns" : [ "last(collect(path))" ],
"data" : [ [ {
"start" : "http://localhost:7474/db/data/node/3907",
"nodes" : [ "http://localhost:7474/db/data/node/3907", "http://localhost:7474/db/data/node/3906", "http://localhost:7474/db/data/node/3905", "http://localhost:7474/db/data/node/3904" ],
"length" : 3,
"relationships" : [ "http://localhost:7474/db/data/relationship/4761", "http://localhost:7474/db/data/relationship/4762", "http://localhost:7474/db/data/relationship/4763" ],
"end" : "http://localhost:7474/db/data/node/3904"
} ] ]
}
How would one convert the cypher query to Neo4JClient query?
Well, the thing you are returning is a PathsResult not a Node<>, so if you change your query to be:
var k = clientConnection.Cypher
.Start(new { n = "node:node_auto_index(Name = 'user1')" })
.Match("path = (n-[r:IS_MEMBER_OF_GROUP*]->b)")
.ReturnDistinct<PathsResult>("last(collect(path))").Results; //<-- Change here
you will get results, this returns what I get from running your query against my db, if you specifically want the nodes this post: Getting PathsResults covers converting to actual nodes and relationships.
One other thing, (and this will help the query perform better as neo4j can cache the execution plans easier), is that you can change your start to make it use parameters by doing:
.Start(new { n = Node.ByIndexLookup("node_auto_index", "Name", "user1")})
By default the sort on Solr facet fields is by "count" and not by "index". I have facet fields such as "date", "author", "places" etc. I want the default sort (sort by count/ sort = true) to be applicable only for "author" and "places" facet fields and I want the sort to be changed to sort by "index" (sort = false) for the "date" field. That way, date is sorted in order of the year and author, places are sorted by the count.
I have this piece of code to work with,
Facet = new FacetParameters
{
Queries = AllFacetFields.Except(SelectedFacetFields(parameters))
.Select(f => new SolrFacetFieldQuery(f) { MinCount = 1})
.Cast<ISolrFacetQuery>()
.ToList(),
},
If I include "Sort = false" in the .Select line above, it affects all the facet fields to change to sort by index. I want to apply "Sort = false" only for facet field "date". How to accomplish that? I am a beginner with linq or solrnet. Please help
In order to achieve the desired behavior, you should split the three facet queries into separate queries where you can control the sort behavior individually.
Facet = new FacetParameters
{
Queries = new []
{
new SolrFacetFieldQuery("author") {MinCount = 1 },
new SolrFacetFieldQuery("places") {MinCount = 1 },
new SolrFacetFieldQuery("date") {MinCount = 1, Sort = false }
}
}
For further information please see the Facets page on the SolrNet Google Code Project Site.
Update: Since there was some additional logic in the original LINQ query, you could do something like the following to achieve a combination of both:
Facet = new FacetParameters
{
Queries = AllFacetFields.Except(SelectedFacetFields(parameters))
.Select(f => BuildFacetQuery(f))
.Cast<ISolrFacetQuery>()
.ToList(),
},
private SolrFacetFieldQuery BuildFacetQuery(string fieldName)
{
var query = new SolrFacetFieldQuery(fieldName) { MinCount = 1 };
if(string.Compare(fieldName, "date", StringComparison.OrdinalIgnoreCase) == 0)
query.Sort = false;
return query;
}
I have the following SQL command:
SELECT CONVERT(varchar, Logged, 103) AS Visited, COUNT(ID) AS Totals
FROM tblStats
GROUP BY CONVERT(varchar, Logged, 103)
ORDER BY Visited DESC
I want to translate this into a L2S statement that can be used with the Entity Framework, but in working with datetime types, I'm getting various errors depending on how I try to attack the problem.
Approach:
var results = from s in db.Stats
group s by (s.Logged.Date.ToString()) into Grp
select new { Day = Grp.Key, Total = Grp.Count() };
Error:
LINQ to Entities does not recognize
the method 'System.String ToString()'
method, and this method cannot be
translated into a store expression.
Approach:
var results = from s in db.Stats
group s by (s.Logged.Date) into Grp
select new { Day = Grp.Key, Total = Grp.Count() };
Error:
The specified type member 'Date' is
not supported in LINQ to Entities.
Only initializers, entity members, and
entity navigation properties are
supported.
What syntax do I need to make the query work?
Try using the EntityFunctions.TruncateTime method:
var results = from s in db.Stats
group s by EntityFunctions.TruncateTime(s.Logged) into Grp
select new { Day = Grp.Key, Total = Grp.Count() };
Do you need the Date section in s.Logged.Date?
Have you tried this:
var results = from s in db.Stats
group s by (s.Logged) into Grp
select new { Day = Grp.Key, Total = Grp.Count() };
I'm assuming that Logged is the property (column in the table).
EDIT: you guys are way to quick.