How to get column names from a table returned from a stored procedure

I have a stored procedure that returns a table. The stored proc is called through the linq datacontext.

It works fine, and I am returning the table, but I really want to get a name that relates to each cell returned.

Does anyone know how to do this?

A stored procedure call looks like this:

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

So, I get List<GetTable>. The data is fine, I just want the column names.

+3
source share
2 answers

You can use reflection for this.

var columns = table.First();

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

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

You can also use the Meta model in 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);

}

, , , , .

+5

. , , 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();
}
+1

Source: https://habr.com/ru/post/1791331/


All Articles