How to set more that one node property using Cypher in Memgraph? - memgraphdb

When I want to set a node property value, I use
MATCH (c:City)
WHERE c.name = 'London'
SET c.population = 8900000
RETURN c;
How can I set more than one property within code block?

To set more than one property divide the properties with coma, like this:
MATCH (c:City)
WHERE c.name = 'London'
SET c.population = 8900000, c.country = 'United Kingdom'
RETURN c;

Related

Linq request group by

I'd like to group my request by FkOperateur and FkPstStd but I still need information in the select new part. How should I proceed to get IdPoste, FkStandard, IsValidated, Date depending on the group part result
var shortFormations =
(from f in _context.Formations
where f.FkPstStdNavigation.FkPosteNavigation.FkZoneNavigation.FkBatimentNavigation.IdBatiment == Batiment && (f.FkOperateurNavigation.Equipe == Equipe || Equipe == null)
group f by new { f.FkOperateur, f.FkPstStd } into formation
select new shortFormations
{
FkOperateur = formation.Key.FkOperateur,
FkPstStd = formation.Key.FkPstStd,
IdPoste = f.FkPstStdNavigation.FkPosteNavigation.IdPoste,
FkStandard = f.FkPstStdNavigation.FkStandard,
IsValidated = f.IsValidated,
Date = f.DateFormation,
})
.AsNoTracking()
.ToList();
I can't get IdPoste FkStandard IsValidated and Date
return f doesn't exist on current context on these line.
Could you explain me please.
Select
fk_operateur,Fk_Pst_Std, max(date_formation)
FROM
Formations
GROUP by
Fk_Operateur,Fk_Pst_Std
that's a try in SQL but I'm not able to get others column mentionned before
returns
fk_operateur
FkPstStd
date_formation
1
1
Adate
and many other rows
Since its grouped, to access the mentioned fields, you will have to look in the Values and select the correct item. As in, something like
select new shortFormations
{
FkOperateur = formation.Key.FkOperateur,
FkPstStd = formation.Key.FkPstStd,
IdPoste = formation.FirstOrDefault()
.FkPstStdNavigation.FkPosteNavigation.IdPoste,
....
or order it and then select the correct record

How to create a temporary column + when + order by with Criteria Builder

here is the sql statement I am trying to translate in jpa :
select
id,
act_invalidation_id,
last_modification_date,
title,
case when act_invalidation_id is null then 1 else 0 end as test
from act order by test, last_modification_date desc
The actual translation
Root<Act> act = query.from(Act.class);
builder.selectCase()
.when(builder.isNull(actRoot.get("actInvalidation")), 1)
.otherwise(0).as(Integer.class);
Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
Order byInvalidationOrder = builder.asc(actInvalidationPath);
Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
Order byLastModificationDate = builder.desc(publicationDate);
query.select(act).orderBy(byInvalidationOrder, byLastModificationDate);
entityManager.createQuery(query).getResultList();
I try to create a temporary column (named test) of Integer type and orderby this column, then orderby lastmodificationdate. The content of this new column is determined by the value of actInvalidation field.
In short: How to create a temp column with integer values, then order by this temp column in jpa ?
Thank you
I didn't test this but it should work like this:
Root<Act> act = query.from(Act.class);
Expression<?> test = builder.selectCase()
.when(builder.isNull(actRoot.get("actInvalidation")), 1)
.otherwise(0).as(Integer.class);
Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
Order byInvalidationOrder = builder.asc(actInvalidationPath);
Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
Order byLastModificationDate = builder.desc(publicationDate);
Order byTest = builder.asc(test);
query.select(act).orderBy(byTest, byInvalidationOrder, byLastModificationDate);
entityManager.createQuery(query).getResultList();

Distinct works on IQueryable but not List<T>?? Why?

First Table is the View and Second is the result I want
This below query works fine
List<BTWStudents> students = (from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
select new BTWStudents
{
StudentId = V.StudentId
Amount= V.PaymentMethod == "Cashier Check" ? V.Amount: "0.00"
}).Distinct().ToList();
But I changed it to List to add string formatting(see below)
List<BTWStudents> students = (from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
select new {V}).ToList().Select(x => new BTWStudents
{
StudentId = V.StudentId
Amount= V.PaymentMethod == "Cashier Check" ? String.Format("{0:c}",V.Amount): "0.00"
}).Distinct().ToList();
With this Second Query I get this
Why is distinct not working in the second query?
When working with objects (in your case a wrapped anonymous type because you are using Select new {V} rather than just Select V), Distinct calls the object.Equals when doing the comparison. Internally, this checks the object's hash code. You'll find in this case, the hash code of the two objects is different even though the fields contain the same values. To fix this, you will need to override Equals on the object type or pass a custom IEqualityComparer implementation into the Distinct overload. You should be able to find a number of examples online searching for "Distinct IEqualityComparer".
Try this (moved your distinct to the first query and corrected your bugged if/then/else):
List<BTWStudents> students = (from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
select new {V}).Distinct().ToList().Select(x => new BTWStudents
{
classId = V.Class.HasValue ? V.Class.Value : 0,
studentName = V.StudentName,
paymentAmount = V.PaymentMethod == "Cashier Check" ? String.Format("{0:c}",x.V.AmountOwed): "0.00"
}).ToList();
You can get around using Distinct all together if you Group by StudentID
var studentsGroupedByPayment =
(from V in db.vwStudentCoursesSD
where classIds.Contains(V.Class.Value)
group V by V.StudentId into groupedV
select new
{
StudentID = groupedV.Key,
Amount = string.Format("{0:C}",
groupedV.First().PaymentMethod == "Cashier Check" ?
groupedV.First().Amount : 0.0)
}
).ToList();

anonymous type and multiple properties

I have this error: An anonymous type cannot have multiple properties with the same name. Whether this can be resolved with alias ie, whether an alias exists in LINQ
var device_query = from d in DevicesEntities.device
join dt in DevicesEntities.devicetype on d.DeviceTypeId equals dt.Id
join l in DevicesEntities.location on d.Id equals l.DeviceId
join loc in DevicesEntities.locationname on l.LocationNameId equals loc.Id
where l.DeviceId == d.Id
select new {
d.Id,
d.DeviceTypeId,
d.SerialNumber,
d.FirmwareRev,
d.ProductionDate,
d.ReparationDate,
d.DateOfLastCalibration,
d.DateOfLastCalibrationCheck,
d.CalCertificateFile,
d.Notes,
d.TestReportFile,
d.WarrantyFile,
d.CertificateOfOriginFile,
d.QCPermissionFile,
d.Reserved,
d.ReservedFor,
d.Weight,
d.Price,
d.SoftwareVersion,
dt.Name,
dt.ArticleNumber,
dt.Type,
l.StartDate, //AS LastStartDate,
l.LocationNameId,
loc.Name //in this line I have problem
};
You need to provide alternative names for the duplicate properties. For example:
select new {
// ... other properties here ...
dt.Name,
dt.ArticleNumber,
dt.Type,
LastStartDate = l.StartDate,
l.LocationNameId,
CurrentLocation = loc.Name
};
It's a confligt on "Name"
You are mapping both dt.Name and loc.Name, both which the compiler tries to set to the "Name" property of the anon type.
change one of them to e.g. LoationName = loc.Name.
HTH
[edit]
Way too late to hit submit :-)

How can I get my orderby to work using an anonymous type?

What do I put in my order by?? I want to order by Name. I have moved the orderby after the distinct because I read that it needs to be done last.
var result = (from r in db.RecordDocs
where r.RecordID == recordID
select new
{
DocTypeID = r.Document.DocType.DocTypeID,
Name = r.Document.DocType.Name,
Number = r.Document.DocType.Number
}
).Distinct().OrderBy( );
Just do
.OrderBy(doc => doc.Name)
Another option, if you really prefer the query expression syntax would be to chain your query construction across multiple statements:
var query = from r in db.RecordDocs
where r.RecordID == recordID
select new
{
DocTypeID = r.Document.DocType.DocTypeID,
Name = r.Document.DocType.Name,
Number = r.Document.DocType.Number
};
query = query.Disctinct();
query = from doc in query orderby doc.Name select doc;
Since all of these methods are deferred, this will result in the exact same execution performance.

Resources