简体   繁体   中英

Using runtime types with linq

I'm using Readify Neo4jClient to make cypher-queries to a neo4j graph-database.

At some point I want to make a query to the database like this:

var results = Client.Cypher
                 .Match("(entity:" + myEntityType1 + ") -[:" + myRelationshipType + "]- (entity2:" + myEntityType2 + ")")
                 .Where((Entity entity) => entity.Id == pEntity.Id)
                 .Return(entity2 => entity2.As<runtimeType>())
                 .Results;

At compile time I don't have information on the type that will be loaded by the query. The type I want to load from the db is only known on runtime. Is there a way I can make the linq expression (given to the Return()-function) take a runtime type instead of a hard coded type?

The runtime type will be given by a PropertyInfo object.

Edit

I'm sorry. I missed out some important information:

The runtime type will be a type that inherits from a type that is known on compile time. Giving that known type to the linq expression will give me an result. But of course, this result will only contain the fields declared in the inherited type.

The answer is basically, no, for a very simple reason, "What are you going to do with it next?"

Eventually, you will have to store in in a compile-time typed variable (meaning you really know what the type is, you just don't realize it), or you will have to store it in a System.Object for much of it's lifetime anyway, so you might as will start now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM