从对象中提取值 [英] extract values out of object
问题描述
你好,亲爱的读者。
我试图找出EF数据库对象之间的关系
这是完成这项任务的功能:
Hello, dear reader.
I am trying to find out relations between EF Database objects
This is the function to fulfill that task:
private object getCollectionReference(object EFtable, string navigationproperty)
{
object collectionreference = new object();
//now try to query the table
try
{
DbEntityEntry db = ce.Entry(EFtable);
//query collections of referenced tables
collectionreference = db
.Collection(navigationproperty)
.Query()
.AsQueryable();
}
catch (Exception e)
{
//query single referenced table
DbEntityEntry db = ce.Entry(EFtable);
collectionreference = db
.Reference(navigationproperty)
.Query()
.AsQueryable();
}
return collectionreference;
}
在铸造之前效果很好... :-(
对象EFtable可以是我们数据库中的任何实体。
是否有机会将其重新投入所需的EF对象以进行进一步处理?
我读过关于用Reflections解决问题的几十篇文章,这是我到目前为止所做的:
That works pretty well until it comes to casting... :-(
The object EFtable can be any Entity in our database.
Is there any opportunity to cast it back into the desired "EF Object" for further processing it?
I read dozens of articles about solving that with Reflections, here is what I´ve done so far:
//get the referenced table[s]
object referenceLogAction4 = getCollectionReference(us, "Data_Subjects");
//get the type
Type ref4 = referenceLogAction4.GetType();
//load all properties
IList<PropertyInfo> pops = new List<PropertyInfo>(ref4.GetProperties());
List<object> lobj = new List<object>();
foreach (PropertyInfo pop in pops)
{
object popvalue = pop.GetValue(referenceLogAction4, null);
lobj.Add(popvalue);
}
//try loading fields
IList<FieldInfo> fies = new List<FieldInfo>(ref4.GetFields());
List<object> lfie = new List<object>();
foreach (FieldInfo fie in fies)
{
object fiesvalue = fie.GetValue(referenceLogAction4);
lfie.Add(fiesvalue);
}
Propertyinfo和Fieldinfo似乎都不是我想要的; Fieldinfo部分返回零Fieldinfos,Propertyinfo值为:
it
转换后的SQL语句
[System.Data.Entity.Core.Objects.ObjectContext]
AppendOnly
true
[System.Data.Entity.Core。 Objects.ObjectParameterCollection]
true
我觉得这些是对象类型的标准属性...
我也尝试通过
neither Propertyinfo nor Fieldinfo seems to be what I want; the Fieldinfo part returns zero Fieldinfos, the Propertyinfo values are:
"it"
"Transformed SQL Statement"
[System.Data.Entity.Core.Objects.ObjectContext]
AppendOnly
true
[System.Data.Entity.Core.Objects.ObjectParameterCollection]
true
Seems to me that these are the standard properties of type object...
I also tried casting it via
ToType<T>(this object obj, T type) where T : class
...
但无济于事...
所以,简而言之:
有谁知道,我需要做什么才能将对象转换为我想要的EF实体对象?
非常感谢,
Clodetta
but to no avail...
so, to put long things short:
does anybody know, what I need to do to cast the object to my desired EF entity object?
Thanks a lot in advance,
Clodetta
推荐答案
大家好。
我终于找到了解决方案:jig:
Hi, everybody.
I finally found a solution :jig:
object referenceLogAction4 = getCollectionReference(us, "Data_Subjects");
现在可以使用此片段转换referenceLogAction4:
referenceLogAction4 can now be converted using this snippet:
List<Data_Subjects> dsubj = (((System.Data.Entity.Core.Objects.ObjectQuery<Data_Subjects>)referenceLogAction4).ToList());
etvoilá,我现在可以迭代通过所有退回的表格参考....
亲切的问候,
Clodetta
PS:我如何将这个问题标记为已解决?
PPS:它将自己标记为解决方案......
et voilá, I now can iterate through all returned table references....
kind regards,
Clodetta
PS: How can I mark this question as solved?
PPS: It marked itself as solution...
这篇关于从对象中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!