Neo4j-如何使用c#client cypher使用WITH子句

时间:2017-03-27 11:03:10

标签: c# neo4jclient

我试图在C#neo4j客户端使用WITH子句获得结果 由于我是neo4j的新手,我对如何在C#中获得结果一无所知。
请帮助我提出建议

MATCH (cs:CrawlerInfo)
WITH cs, SIZE((cs)-[:CONTAINS]->()) as TotalResult
RETURN 
cs.CrawlerName,cs.Keyword,cs.SearchType,toint(TotalResult),cs.CrawlerInfoDate
order by toint(cs.CrawlerId) desc

1 个答案:

答案 0 :(得分:1)

Neo4jClient直接翻译是关键,应该可以通过API发现。

//gc = GraphClient instance
//CrawlerInfo is a class
gc.Cypher
    .Match("(cs:CrawlerInfo)")
    .With("cs, SIZE( (cs)-[:CONTAINS]->() ) as TotalResult")
    .Return((cs, TotalResult) => new
    {
        CrawlerName = cs.As<CrawlerInfo>().CrawlerName,
        Keyword = cs.As<CrawlerInfo>().Keyword,
        SearchType = cs.As<CrawlerInfo>().SearchType,
        CrawlerInfoDate = cs.As<CrawlerInfo>().CrawlerInfoDate,
        Size = Return.As<int>("toint(TotalResult)")
    })
    .OrderByDescending("toint(cs.CrawlerId)");

就个人而言,我会让我的CrawlerInfo课程看起来像:

public class CrawlerInfo
{
    public int CrawlerId { get; set;}
    public string CrawlerName { get; set; }
    public string Keyword { get; set; }
    public string SearchType { get; set; }
    public string CrawlerInfoDate { get; set;}
}

然后你也没有/(不应该!)需要做toint,这意味着你可以这样做:

gc.Cypher
    .Match("(cs:CrawlerInfo)")
    .With("cs, SIZE( (cs)-[:CONTAINS]->() ) as TotalResult")
    .Return((cs, TotalResult) => new
    {
        CrawlerName = cs.As<CrawlerInfo>().CrawlerName,
        Keyword = cs.As<CrawlerInfo>().Keyword,
        SearchType = cs.As<CrawlerInfo>().SearchType,
        CrawlerInfoDate = cs.As<CrawlerInfo>().CrawlerInfoDate,
        Size = Return.As<int>("TotalResult")
    })
    .OrderByDescending("cs.CrawlerId");

哪个有点整洁。我也很想回复CrawlerInfo对象本身,而不是属性(或者如果CrawlerInfoLite是大量的话,还要制作一个小的CrawlerInfo模型):

gc.Cypher
    .Match("(cs:CrawlerInfo)")
    .With("cs, SIZE((cs)-[:CONTAINS]->()) as TotalResult")
    .Return((cs, TotalResult) => new
    {
        CrawlerInfo = cs.As<CrawlerInfo>(),
        Size = Return.As<int>("TotalResult")
    })
    .OrderByDescending("cs.CrawlerId");

CrawlerInfo定义为:

public class CrawlerInfo
{
    public int CrawlerId { get; set;}
    public string CrawlerName { get; set; }
    public string Keyword { get; set; }
    public string SearchType { get; set; }
    public string CrawlerInfoDate { get; set;}
}