Selecting Related Entities When No Association Exists - entity-framework-4

EF4 is great for pulling in associated data but what do you do when the association is not explicit? An example illustrates my situation:
MasterTable has a child1Id and child2Id column.
There are two tables Child1 and Child2 with corresponding primary key child1Id and child2Id. There are Master, Child1 and Child2 entities.
There is no foreign key or entity framework association between Master and Child1 / Child2 tables or entities.
How can I select the master records and corresponding child records from the two child tables when all I have are the matching child Ids in the master?
I can't retrofit a relationship or association.
Richard

You must select them manually by linq to entities. Here is how to do left join between two tables:
var query = from m in context.Masters
where m.Id == 1
join c in context.Childs on m.Child.Id equals c.Id into leftJoin
from x in leftJoin.DefaultIfEmpty()
select new
{
Id = x.Id,
Name = x.Name,
Child = x.Childs
};
Btw. if your entities have a property which contains a value of PK from other entity you can create relation in EF designer. In such case you will be able to use navigation properties.

Related

Joins not working due to missing properties in Neo4J

I imported data into the neo4j database and there were some blank values as well so while importing the data, the property didn't create into the nodes which have blank values. i.e.
Student
Name | City
Amit | Delhi
Akshay |
So 2 nodes are being created. 1 node has 2 properties and another node has a single property.
I created a relationship something like:-
LOAD CSV WITH HEADERS FROM "file:///Student.csv" AS row
MATCH (e:College {College_ID: row.College_ID})
MATCH (c:Student {Name: row.Name,City: row.City})
MERGE (c)-[:REQ_TestedBy_TC]->(e);
Now when I'm going to join these nodes like:-
(c:College)-[r:CollegeHaveStudent]->(s:Student)
Then it is returning only the first row because the second student doesn't contain the City property so Join is not working.
I need help with what can be a workaround for that situation.

How to insert multiple properites with same name but different values in neo4j relation?

I have 2 nodes, person and job.The relation between them is VIEWED.
I need to store the list of timestamps the person viewed the job in the properties of VIEWED relation.
The best way to do this would be to use additional nodes (with label :Viewing) with a relationship to the :Person and the :Job nodes.
With Cypher, you can use MERGE to ensure that only a single VIEWED relationship exists between a Person and Job pair, and the ON CREATE and ON MATCH clauses to either initialize or append to the relationship's timestamp list.
For example:
MATCH (p:Person), (j:Job)
WHERE p.id = 123 AND j.id = 987
MERGE (p)-[r:VIEWED]->(j)
ON CREATE SET r.times = [datetime()]
ON MATCH SET r.times = r.times + datetime()

Create Relationship Between three tables in Cypher

Is it possible to create relationships between table1 with table3 by using table2?
for example in:
Table1 we have id and momName
Table2 we have id and table1ID and Table3ID
Table3 we have id and dadName
we need to create relationship between mom and dad directly by just one type of relationship [:family] without showing the table2 nodes or relations.
This should work:
MATCH (t1:Table1), (t2:Table2), (t3:Table3)
WHERE t1.id = t2.table1ID AND t3.id = t2.table3ID
MERGE (t1)-[:family]-(t3)
For faster performance, you can first create indexes on :Table1(id) and :Table3(id).

Neo4J - How to create relationship based on an attribute value - if exists or not?

In an existing relationship I want to add an additional one. The new relation should depend on a specific attribute value.
E.g. if an Employee has a value -1 in department_id property, then he should have a relationship: MEMBER_OF to department table, if he has 1, 2 or 3, then he should have a relation as HEAD_OF.
And I think the direction (arrow) for relationships would be opposite to each other -[:HEAD_OF]-> or <-[:MEMBER_OF]-
One important thing to add is that the value -1 doesn't exist in the department graph. The dep graph has IDs just from 1 till 8.
In other words, this could be something like: if any employee has an ID which doesn't exist in dep Graph... in this case -1
EDIT
After discussing by chat, the queries that best fits the requirements is:
The first query create a :HEAD_OF relationship between all employees that have department_id <> -1 and the respective department.
MATCH (emp:Employee)
WHERE emp.department_id <> -1
MATCH (dep:Department)
WHERE dep.id = emp.department_id
CREATE (emp)-[:HEAD_OF]->(dep)
The second query will create a relation :MEMBER_OF between employees that have department_id = -1 and a random department.
MATCH (emp:Employee {department_id : -1})
MATCH (randomDep:Department)
WITH collect(emp) as emps, randomDep LIMIT 1
UNWIND emps as emp
CREATE (emp)-[:MEMBER_OF]->(randomDep)

Count associations from a where clause active record

I have a list of Parents who have many Children and each Child has one and only one skill. The parents have a many-to-many relationship to children and Children can have the same skill.
I am trying to get a list of all parents and add a count field of how many children they have with a particular skill. Is this possible with active record?
My current solution uses this Parent.joins(:children).select('parents.*, COUNT(*) AS child_count').group('parents.id').where(children: {skill_name: skill})
This however doesn't return Parents with a count of 0 for child_count. Is there any way to accomplish this with Active Record? I want to return JSON for every parent with a count of how many of their children have a specific skill.
.joins(:children) generates an INNER JOIN which will exclude parents with 0 children. You should change it to an OUTER JOIN. For the most part you cannot do this with ActiveRecord (unless you want to perform eager loading, too). Assuming that your join table is named parents_children, then you can build the join yourself:
.joins("LEFT OUTER JOIN parents_children ON parents_children.parent_id = parents.id INNER JOIN children ON parents_children.child_id = children.id")

Resources