如何从存储过程返回的表中获取列名

时间:2011-02-10 21:56:25

标签: c# asp.net sql linq stored-procedures

我有一个返回表的存储过程。存储过程通过linq datacontext调用。

它工作正常,我把桌子拿回去,但我真的想得到与每个特定单元格相关的标题。

有谁知道怎么做?

存储过程调用如下:

var table = DataContext.GetTable().ToList();

所以我得到了List<GetTable>。数据很好我只想要列名。

2 个答案:

答案 0 :(得分:5)

您可以使用反射来执行此操作

var columns = table.First();

var properties = (from property in columns.GetType().GetProperties()
                  select property.Name).ToList();

foreach (var property in properties)
    Console.WriteLine(property);

您还可以使用System.Data.Linq.Mapping Namespace

中的元模型
AttributeMappingSource mappping = new System.Data.Linq.Mapping.AttributeMappingSource();
var model = mappping.GetModel(typeof(MyDataContext));


MetaFunction function = model.GetFunction(typeof(MyDataContext).GetMethod("MyStoredProc"));


foreach (var resultTypes in function.ResultRowTypes)
{

    foreach (var column in resultTypes.DataMembers)
    Console.WriteLine(column.Name);

}

由于存储过程可以有多个结果集,因此它可能是处理该情况的更好方法。

答案 1 :(得分:1)

您可以尝试在实体类型上使用反射。据我所知,如果它们具有关联的ColumnAttribute,则所有生成的属性都对应于表中的列。你可以试试这个:

public static List<string> GetColumnNames<TEntity>(Table<TEntity> table)
    where TEntity : class
{
    return GetColumnNames(typeof(TEntity));
}

public static List<string> GetColumnNames(DataContext context, string functionName)
{
    var retType = context.GetType().GetMethod(functionName).ReturnType;
    System.Diagnostics.Debug.Assert(retType.Name == "ISingleResult`1");
    return GetColumnNames(retType.GetGenericArguments().Single());
}

public static List<string> GetColumnNames(Type entityType)
{
    return (from p in entityType.GetProperties()
            let columnAttribute = p.GetCustomAttributes(false)
                                   .OfType<System.Data.Linq.Mapping.ColumnAttribute>()
                                   .SingleOrDefault()
            where columnAttribute != null
            select columnAttribute.Name ?? p.Name)
           .ToList();
}

// usage:
// from a function/procedure name
var names1 = GetColumnNames(DataContext, "GetTable");
// or by entity type directly (the return type of the function/procedure)
var names2 = GetColumnNames(typeof(GetTable));

鉴于看到康拉德使用元模型,我想出了这个。需要过滤掉关联(由LINQ to SQL添加)以从表中获取列名。

public static List<string> GetColumnNames<TEntity>(Table<TEntity> table)
    where TEntity : class
{
    return new System.Data.Linq.Mapping.AttributeMappingSource()
        .GetModel(table.Context.GetType())
        .GetTable(typeof(TEntity))
        .RowType
        .DataMembers
        .Where(dm => !dm.IsAssociation)
        .Select(dm => dm.MappedName)
        .ToList();
}

public static List<string> GetColumnNamesMeta(DataContext context, string functionName)
{
    var type = context.GetType();
    return new System.Data.Linq.Mapping.AttributeMappingSource()
        .GetModel(type)
        .GetFunction(type.GetMethod(functionName))
        .ResultRowTypes
        .SelectMany(rrt => rrt.DataMembers
                              .Where(dm => !dm.IsAssociation)
                              .Select(dm => dm.MappedName))
        .ToList();
}