C# 获取所选表中的所有列名称,前提是使用Entity Framework 4.0将类名作为字符串传递
我想获取所选数据库中所有列的名称,前提是要获取的列的表的名称在字符串数组中,并且可以动态更改 我有一个代码,在该代码中,我只能获取特定表中所有列的名称:C# 获取所选表中的所有列名称,前提是使用Entity Framework 4.0将类名作为字符串传递,c#,asp.net,entity-framework,linq-to-entities,C#,Asp.net,Entity Framework,Linq To Entities,我想获取所选数据库中所有列的名称,前提是要获取的列的表的名称在字符串数组中,并且可以动态更改 我有一个代码,在该代码中,我只能获取特定表中所有列的名称: var properties = from p in typeof(EntityAttribute).GetProperties() where (from a in p.GetCustomAttributes(false)
var properties = from p in typeof(EntityAttribute).GetProperties()
where (from a in p.GetCustomAttributes(false)
where a is EdmScalarPropertyAttribute
select true).FirstOrDefault()
select new
{
FieldName = p.Name,
FieldType = p.PropertyType,
FieldPK = p.GetCustomAttributes(false).Where(a => a is EdmScalarPropertyAttribute && ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() > 0
};
EntityAttribute是列的名称
问题是这个类名EntityAttribute是一个类。我想从字符串数组中传递这个类名
示例:我有一个字符串数组,其名称为三个表:
现在我想通过传递字符串中的类名来迭代上面给出的代码
我应该如何传递它?您可以使用
GetType
Type type = Type.GetType("YourApplication.Namespace.EntityAttribute");
您是否使用了完全限定字符串
YourApplication.Namespace.EntityAttribute
例如,更新了我的回答,因为我犯了错误。谢谢你,真是太棒了。很高兴我能帮忙!