如何在.NET Core API中编写指定Cypher查询以获取返回结果
首先确保你的learningPaths实体类中定义了对应类型的modules属性:
public class learningPaths { // 其余属性省略 public IEnumerable<string> modules { get; set; } }
实现方式1:基于LINQ表达式转换
Neo4jClient支持将C#的Any方法自动转换为Cypher的any函数,写法和你之前的风格一致:
var targetCourse = "any course"; var result = ( await _graphClient.Cypher .Match("(n:learningPaths)") .Where((learningPaths n) => n.modules.Any(x => x == targetCourse)) .Return(n => n.As<learningPaths>()) .ResultsAsync) .ToList();
实现方式2:直接写Cypher查询片段(更稳妥,推荐)
如果遇到LINQ转换不兼容的情况,可以直接写原生Cypher条件,配合参数化查询避免注入问题:
var targetCourse = "any course"; var result = ( await _graphClient.Cypher .Match("(n:learningPaths)") .Where("any(x IN n.modules WHERE x = $targetCourse)") .WithParam("targetCourse", targetCourse) .Return(n => n.As<learningPaths>()) .ResultsAsync) .ToList();
内容的提问来源于stack exchange,提问作者Prateek
相关产品推荐
相关产品推荐

