需要列名称来自实体框架使用字符串表名称c#

时间:2018-04-20 06:41:42

标签: c# sql asp.net entity-framework linq

public List<string> GetColumnsForTable(string table)
    {
        var context = _unitOfWork.GetContext();

        var names = typeof(table).GetProperties()
                .Select(property => property.Name)
                .ToArray();

        return names ;
    }

实体对象的需要类型,但我从下拉列表中选择了字符串

typeof(table).GetProperties()

在c#

中是否有其他方法可以从实体框架中获取列

1 个答案:

答案 0 :(得分:0)

一个例子。您可以使用反射来获得正确类型的对象

让我们说对象在namespace People.Family

和班级Dad

Type myType = Type.GetType("People.Family.Dad");
var names = myType.GetProperties().Select(pro => pro.Name).ToArray();

您仍然会面临必须指定所有对象命名空间和所有对象命名空间的问题。希望它能让你了解什么是可能的。