I'm trying to search for a collection of potential nodes but unable to do it...
I have a product that has a relationship with many instances. I would like to query the DB and get all the instances that are in a list that i get from the user.
Cypher:
var query = _context
.Cypher
.Start(new
{
instance = startBitsList,
product = productNode.Reference,
})
.Match("(product)-[:HasInstanceRel]->(instance)")
.Return(instance => instance.Node<ProductInstance>());
The problem is startBitsList... I use StringBuilder to generate a query that contains all the instances I'm looking for:
private static string CreateStartBits(IEnumerable<string> instanceNames)
{
var sb = new StringBuilder();
sb.AppendFormat("node:'entity_Name_Index'(");
foreach (var id in productIds)
{
sb.AppendFormat("Name={0} OR ", id);
}
sb.Remove(sb.Length - 4, 4);
sb.Append(")");
var startBitsList = sb.ToString();
return startBitsList;
}
I get exceptions when trying to run this cypher...
Is there a better way to search for multiple items that are stored in the collection I get from the user?
OK, I think there are a couple of issues at play here, first I'm presuming you are using Neo4j 1.9 and not 2.0 - hence using the .Start.
Have you tried taking your query and running it in Neo4j? This should be your first port of call, typically it's easy to add a breakpoint on the .Results call and add a 'watch' for query.Query.DebugText.
However, I don't think you need to use the StartBits the way you are, I think you'd be better off filtering with a .Where as you already have the start point:
private static ICypherFluentQuery CreateWhereClause(ICypherFluentQuery query, ICollection<string> instanceNames)
{
query = query.Where((Instance instance) => instance.Name == instanceNames.First());
query = instanceNames.Skip(1).Aggregate(query, (current, localInstanceName) => current.OrWhere((Instance instance) => instance.Name == localInstanceName));
return query;
}
and your query becomes something like:
var prodReference = new NodeReference<Product>(2);
var query =
Client.Cypher
.ParserVersion(1, 9)
.Start(new {product = prodReference})
.Match("(product)-[:HasInstanceRel]->(instance)");
query = CreateWhereClause(query, new[] {"Inst2", "Inst1"});
var resultsQuery = query.Return(instance => instance.As<Node<Instance>>());
2 things of note
We're not using the indexes - there is no benefit to using them as you have the start point and traversing to the 'instances' is a simple process for Neo4j.
The 'CreateWhereClause' method will probably go wrong if you pass in an empty list :)
The nice thing about not using the indexes is that - because they are legacy - you are set up better for Neo4j 2.0
Related
I have a method of type IQueryable<T> and here is it's implementation:
public IQueryable<T> Get()
{
return db.Set<T>();
}
I must write LINQ query where I want to join two tables (left join). It is Identtiy table Users and my custom table PersonalInformation which extend User registration fields and now I want to call this method in my LINQ query and it is good. Here is my Linq:
IRepository<PersonalInformation> personRepository;
IRepository<ApplicationUser> usersRepository;
var query = personRepository.Get();
var test = usersRepository.Get()
.GroupJoin(query,
n => n.Id,
m => m.UserId,
(n, ms) => new { n, ms = ms.DefaultIfEmpty() })
.SelectMany(z => z.ms.Select(m => new PersonalInfoModel
{
Name = m.Name,
LastName = m.LastName,
Email = z.n.Email
}));
But I have an error in
var test = usersRepository.Get() - System.NotSupportedException. So method Get from personRepository called good but usersRepository method return null. Where I did the mistake?? Thanks
It looks likely that you are having an error combining queries from two different database contexts. Your custom PersonalInformation is probably in a custom DBContext while Users is in the IdentityDBContext. See this related question. You can either:
Move all of your tables into the same context.
Avoids future confusion between these tables
More efficient if you end up with lots of associations across contexts.
A more involved solution just to get this one example working.
Query your tables separately and combine in memory.
Less scalable if you have a huge number of users.
These operators will cause EF to return the results so you can process in memory.
var people = personRepository.Get().ToList();
var users = usersRepository.Get().ToList();
var infoModels = users.GroupJoin(people,
u => u.Id,
p => p.UserId,
(u, mp) => new { n, ms = ms.DefaultIfEmpty() })
.SelectMany(z => z.ms.Select(m => new PersonalInfoModel
{
Name = m.Name,
LastName = m.LastName,
Email = z.n.Email
}));
I read many questions on this topic and created the following almost dynamic query:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => new {
movie = Return.As<string>("movie.title")
}).Results;
Unfortunately this isn't dynamic since I'm declaring the movie property in the Return anonymous type.
In all the examples I found the only option is to return the nodes as an object matches the node properties,
like: movie = Return.As<string>("movie.title")
I want the Return statement to give me back a key-value pair list of all the node properties (it can be in any representation like JSON etc..), since
my nodes are generic and not from a specific object kind every time.
is that possible?
You can do something like this:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => Return.As<Node<Dictionary<string,string>>>("movie"));
var results = resQuery.Results.Select(r => r.Data);
Console.WriteLine(results.First()["title"]);
Alternatively, something like:
var resQuery = WebApiConfig.GraphClient.Cypher
.Match("(movie:Movie {title:{title}})")
.WithParam("title", title)
.Return(() => Return.As<Node<string>>("movie"));
var results = resQuery.Results;
List<dynamic> nodes = results.Select(r => JsonConvert.DeserializeObject<dynamic>(r.Data)).ToList();
Console.WriteLine(nodes[0].title);
Below is the code in question. I receive Object reference not set to an instance of an object. on the where clause inside the Linq query. However, this only happens after it goes through and builds my viewpage.
Meaning: If I step through using debugger, I can watch it pull the correct order I am filtering for, go to the correct ViewPage, fill in the model/table with the correct filtered item, and THEN it comes back to my Controller and shows me the error.
public ActionResult OrderIndex(string searchBy, string search)
{
var orders = repositoryOrder.GetOpenOrderList();
if (Request.QueryString["FilterOrderNumber"] != null)
{
var ordersFiltered = from n in orders
where n.OrderNumber.ToUpper().Contains(Request.QueryString["FilterOrderNumber"].ToUpper().ToString())
select n;
return View(ordersFiltered);
}
return View(orders);
}
its always better to manipulate your strings and other things outside the linq query ,
please refer : http://msdn.microsoft.com/en-us/library/bb738550.aspx
from the readability point of view also its not good ,
public ActionResult OrderIndex(string searchBy, string search)
{
var orders = repositoryOrder.GetOpenOrderList();
var orderNumber = Request.QueryString["FilterOrderNumber"];
if (!string.IsNullOrEmpty(orderNumber))
{
orderNumber = orderNumber.ToUpper();
var ordersFiltered = from n in orders
where n.OrderNumber.ToUpper().Contains(orderNumber)
select n;
return View(ordersFiltered);
}
return View(orders);
}
Your query is not being executed in your Action method because you don't have a ToList (or equivalent) added to your query. When your code returns, your query will be enumerated somewhere in your view and that's the point where the error occurs.
Try adding ToList to your query like this to force query execution in your action method:
var ordersFiltered = (from n in orders
where n.OrderNumber.ToUpper().Contains(Request.QueryString["FilterOrderNumber"].ToUpper().ToString())
select n).ToList();
What's going wrong is that a part of your where clause is null. This could be your query string parameter. Try moving the Request.QueryString part out of your query and into a temporary variable. If that's not the case make sure that your orders have an OrderNumber.
You both were right. Just separately.
This fixed my problem
var ordersFiltered = (from n in orders
where !string.IsNullOrEmpty(n.OrderNumber) && n.OrderNumber.ToUpper().Contains(Request.QueryString["FilterOrderNumber"].ToUpper().ToString())
select n);
I am in the process of learning Durandal and Breeze. And have choosing to create a SPA version of nerddinner.
The first query I need to execute is this:
public IEnumerable<JsonDinner> GetMostPopularDinners(int limit = 10)
{
var mostPopularDinners = from dinner in _db.Context.Dinners.Include("RSVPs")
where dinner.EventDate >= DateTime.Now
orderby dinner.RSVPs.Count descending
select dinner;
if (limit > 50 || limit <= 0)
limit = 10;
return mostPopularDinners.Take(limit).AsEnumerable().Select(JsonDinnerFromDinner);
}
I have started to write it with breeze but I am having trouble with this line " orderby dinner.RSVPs.Count descending" this is what I have so far.
var getMostPopularDinners = function() {
var query = EntityQuery
.from('dinners')
.where('eventDate', '>=', new Date(Date.now()))
.orderByDesc('RSVPs')
.expand('RSVPs');
Sorry, Breeze doesn't yet support ordering or filtering on an aggregate value ('count' in this case).
What you can do is turn this into a named query. (which is not well documented...) Basically this involves using the EntityQuery.withParameters method to pass additional parameters to any service method. So you can construct a query like the following that both passes parameters and still uses Breeze's IQueryable support.
EntityQuery.from("GetMostPopularDinners")
.withParameters({ EventDate: new Date(Date(now()) })
.take(10);
where your controller method would look something like this:
[HttpGet]
public IQueryable<Dinner> GetMostPopularDinners(DateTime eventDate) {
return _db.Context.Dinners.
.Where(dinner => dinner.EventDate >= eventDate)
.OrderByDescending(dinner => dinner.RSVPs.Count);
}
and ... you should not need to do an JsonDinnerFromDinner" call; Breeze handles this automatically.
I'm designing an interface where the user can join a publicaiton to a keyword, and when they do, I want to suggest other keywords that commonly occur in tandem with the selected keyword. The trick is getting the frequency of correlation alongside the properties of the suggested keywords.
The Keyword type (EF) has these fields:
int Id
string Text
string UrlString
...and a many-to-many relation to a Publications entity-set.
I'm almost there. With :
var overlappedKeywords =
selectedKeyword.Publications.SelectMany(p => p.Keywords).ToList();
Here I get something very useful: a flattened list of keywords, each duplicated in the list however many times it appears in tandem with selectedKeyword.
The remaining Challenge:
So I want to get a count of the number of times each keyword appears in this list, and project the distinct keyword entities onto a new type, called KeywordCounts, having the same fields as Keyword but with one extra field: int PublicationsCount, into which I will populate the count of each Keyword within overlappedKeywords. How can I do this??
So far I've tried 2 approaches:
var keywordCounts = overlappingKeywords
.Select(oc => new KeywordCount
{
KeywordId = oc.Id,
Text = oc.Text,
UrlString = oc.UrlString,
PublicationsCount = overlappingKeywords.Count(ok2 => ok2.Id == oc.Id)
})
.Distinct();
...PublicationsCount is getting populated correctly, but Distinct isn't working here. (must I create an EqualityComarer for this? Why doesn't the default EqualityComarer work?)
var keywordCounts = overlappingKeywords
.GroupBy(o => o.Id)
.Select(c => new KeywordCount
{
Id = ???
Text = ???
UrlString = ???
PublicationsCount = ???
})
I'm not very clear on GroupBy. I don't seem to have any access to 'o' in the Select, and c isn't comping up with any properties of Keyword
UPDATE
My first approach would work with a simple EqualityComparer passed into .Distinct() :
class KeywordEqualityComparer : IEqualityComparer<KeywordCount>
{
public bool Equals(KeywordCount k1, KeywordCount k2)
{
return k1.KeywordId== k2.KeywordId;
}
public int GetHashCode(KeywordCount k)
{
return k.KeywordId.GetHashCode();
}
}
...but Slauma's answer is preferable (and accepted) because it does not require this. I'm still stumped as to what the default EqualityComparer would be for an EF entity instance -- wouldn't it just compare based on primary ids, as I did above here?
You second try is the better approach. I think the complete code would be:
var keywordCounts = overlappingKeywords
.GroupBy(o => o.Id)
.Select(c => new KeywordCount
{
Id = c.Key,
Text = c.Select(x => x.Text).FirstOrDefault(),
UrlString = c.Select(x => x.UrlString).FirstOrDefault(),
PublicationsCount = c.Count()
})
.ToList();
This is LINQ to Objects, I guess, because there doesn't seem to be a EF context involved but an object overlappingKeywords, so the grouping happens in memory, not in the database.