Combining return values โ€‹โ€‹into one type

I have a Neo4j relationship that looks like this:

(a:Article)<-[:TRANSLATES]-(t:ArticleTranslation)

      

The label Article

is applied to nodes that simply contain a unique identifier, and the label ArticleTranslation

is applied to nodes that contain:

  • Name
  • Body
  • LanguageCode

Then I have ICypherFluentQuery

one which is built like this:

Match("(t:ArticleTranslation)-[:TRANSLATES]->(a:Article)")
    .Where("(a.Id = {id})")
    .AndWhere("(t.LanguageCode = {lang} OR (t.LanguageCode = 'en' and NOT a<--(:ArticleTranslation { LanguageCode: {lang} })))")
    .WithParams(new { id, lang })

      

From here I understand that I can easily map (a, t)

to an anonymous object containing two object instances, like this:

new { Article = new Article { /* Article Properties */ },
    Translation = new ArticleTranslation { /* Translation Properties */ } }

      

However, in my application it is more useful (and intuitive) to work with both the "article" and the "broadcast" as a single object, for example:

public class Article
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public string LanguageCode { get; set; }
}

      

I managed to do this by adding the following:

.Return((a, t) => new Article
    {
        Id = a.As<Article>().Id,
        Title = t.As<Article>().Title,
        Body = t.As<Article>().Body,
        LanguageCode = t.As<Article>().LanguageCode
    });

      

However, this is cumbersome (especially when increasing the number of properties) and looks like it might cause re-mappings.

Is there a more concise way to express this without introducing an independent object ArticleTranslation

?

+3


source to share


1 answer


You can tell Neo4jClient to create an arbitrary Cypher statement RETURN

- and map it to the type you want - by passing a delegate with no parameters:

.Return(() => Return.As<ArticleTranslation>(
    "{ArticleId:a.Id, Title:t.Title, Body:t.Body, LanguageCode:t.LanguageCode}"))

      



This example returns a literal map and then maps to type ArticleTranslation

Neo4jClient.

0


source







All Articles