0

I would like to fetch the column names from List. As I have a class

public class DetailView

    {
        public string SiteName { get; set; }
        public string ItemType { get; set; }
        public string AssetStorage { get; set; }
    }

and in some method in controller am filling the data into Session. Now I want to get the column names for some reason. Am putting my session data into that list.

List<DetailView> objgrdDtls = new List<DetailView>();
objgrdDtls = (List<DetailView>)Session["datasetVal"]; 

I would like to have the Column name. Pleas note by doing the below code i got the value of that particular column name. restult1 has the column value.

var result1 = objgrdDtls.Where(p => p.SiteName.ToLower().Contains(txt1));

But all i need is Column name. So how do i get that.

stringColumnname = objgrdDtls.get(columnaname => some filter)?

Is this is the way how to get column names? Not sure how to get the column name one by one? Thanks.

leppie
  • 115,091
  • 17
  • 196
  • 297
user3794063
  • 55
  • 2
  • 2
  • 6

2 Answers2

2

You can use reflection to get the column name. Here is the runnable example to get the column name of DetailView.

using System;
using System.Reflection;

public class DetailView
{
     public string SiteName { get; set; }
        public string ItemType { get; set; }
        public string AssetStorage { get; set; }


}

    public class Example
    {
        public static void Main()
        {
            DetailView fieldsInst = new DetailView();
            // Get the type of DetailView.
            Type fieldsType = typeof(DetailView);

        PropertyInfo[] props = fieldsType.GetProperties(BindingFlags.Public 
            | BindingFlags.Instance);


        for(int i = 0; i < props.Length; i++)
        {
            Console.WriteLine("   {0}",
                props[i].Name);
        }
    }
}
Rakib
  • 643
  • 7
  • 17
0

Var result1 will give the list of DetailView Objects which meet the condition "SiteName.ToLower().Contains(txt1)". It will not be a value. Can you please clarify what do you mean by column value. However, For selecting a particular column value, You can append ".Select(p => p.AssetStorage)".

Richa Garg
  • 1,888
  • 12
  • 23
  • Column value is the which it has the data in that, i mean say for example, there is a property SiteName for that property there would be data like 'INDIA'. Now i want to get the Column name ie, property name like 'SiteName', ItemType and AssetStorage which i have declared in class Detail View. – user3794063 Jun 22 '16 at 10:07