使用CollectAs<>时显式转换出错
本文关键字:显式转换 出错 CollectAs 使用 | 更新日期: 2023-09-27 18:04:06
对,我有一个非常简单的问题,但是我无论如何也想不出一个真正简单的答案来解决它。这段代码应该返回一个"Person",其中包含语言和国家的集合。
return client.Cypher
.Match("(person:Person)")
.Where((Person person) => person.Email == username)
.OptionalMatch("(person)-[:SPEAKS]-(language:Language)")
.OptionalMatch("(person)-[:CURRENT_LOCATION]-(country:Country)"
.Return((person, language, country) => new ProfileObject
{
Person = person.As<Person>(),
Language = language.CollectAs<Language>(),
Country = country.CollectAs<Country>()
}).Results.ToList();
它看起来对我来说是正确的,但它不是,在构建时我得到这个错误,我理解但无法解决。
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Neo4jClient.Node<Graph.Country>>' to 'Graph.Country'. An explicit conversion exists (are you missing a cast?)
语言类看起来像这样
public class Language
{
public string Name { get; set; }
}
和ProfileObject类看起来像这样:
public class ProfileObject
{
public Person Person { get; set; }
public Language Language { get; set; }
public Country Country { get; set; }
}
我真的卡住了,请帮忙。
CollectAs
返回一组节点。
您需要将ProfileObject
更改为:
public class ProfileObject
{
public Person Person { get; set; }
public IEnumerable<Node<Language>> Language { get; set; }
public IEnumerable<Node<Country>> Country { get; set; }
}
在即将到来的软件包更新中,Node<T>
包装器已从签名中删除,因此它将只是:
public class ProfileObject
{
public Person Person { get; set; }
public IEnumerable<Language> Language { get; set; }
public IEnumerable<Country> Country { get; set; }
}
如果您现在想要获得这个cleaner签名,请查看NuGet (https://www.nuget.org/packages/Neo4jClient/1.1.0-Tx00009)上的预发布包。