简体   繁体   English

仅在知道关联属性的字符串名称时查询该关联属性

[英]Query a association property when you only know the string name of that property

I have a entity framework class A. It has a 1 to many relationship with B. The property name of this relationship in A is AtoB. 我有一个实体框架类A。它与B具有一对多关系。A中此关系的属性名称为AtoB。

At runtime if I am given a string of "AtoB" is there a way to issue a linq query to A on the AtoB property to get a list of all the B's associated with that A? 在运行时,如果给我一个字符串“ AtoB”,是否可以对AtoB属性的A发出linq查询,以获得与该A关联的所有B的列表?

So basically I won't know until runtime what property on A that I should be running a linq query on. 因此,基本上,直到运行时,我才知道我应该在其上运行linq查询的A的哪个属性。

// Pseudo code
int id = GetID();
string tableName = GetTableName();
var v = context.[tableName].Where(t => t.ID == id);

您可以使用Dynamic Linq执行字符串Linq查询-只需在适当的地方将属性名称嵌入字符串即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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