You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 13:36:04