简体   繁体   中英

How to get Column Names using Linq from DataTable

I'm trying to use LINQ on DataTable that's getting it's data from sql. So I have a data table with it's usual columns and rows and it appears exactly like a sql select statement. Now I need to get certain rows and columns (including column names) from this data.

I converted the datatable to something LINQ can use using AsEnumerable but I'm not sure what exactly it does. Does it convert the data into an array of objects where each row becomes an object?

I'm used to working with Javascript and it's newer arrow functions so i'd like to use Linq with lambda to keep it consistent.

I'm trying to get rows and column names where first column has a value equal to 2018

DataTable myTable = getData(); // populates the datatable and I've verified the data
 var linqTable = myTable.AsEnumerable().Select( x => x[0] = 2018);

I need to get the rows and column names. eg like an object or array of objects.However, the code above doesn't return the data or column names but just two rows with 2018 in it.

My goal is to eventually serialize this data as json and send it to web page.

The problem is Select() is projecting the objects into a new form. You are seeing 2018 because of '=' instead of '=='. You need to use Where()

 var linqTable = myTable.AsEnumerable().Where( x => x.Field<int>(0) == 2018);

You will still end up with a list of DataRows though. The DataTable object isn't really what you should be using because it already provides a nice way to filter its rows:

myTable.Rows.Find(2018);

If you are trying to convert it to a list of objects you should use the Select() method something like:

var linqTable = myTable.AsEnumerable().Where(x => x.Field<int>(0) == 2018)
            .Select(x => new
            {
                year = x[0],
                p1 = x[1],
                p2 = x[2] // etc...
            });

You can create the following function:

public static DataTable CreateDataTableFromAnyCollection<T>(IEnumerable<T> list)
    {
        Type type = typeof(T);
        var properties = type.GetProperties();

        DataTable dataTable = new DataTable();
        foreach (PropertyInfo info in properties)
        {
            dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
        }

        foreach (T entity in list)
        {
            object[] values = new object[properties.Length];
            for (int i = 0; i < properties.Length; i++)
            {
                values[i] = properties[i].GetValue(entity,null);
            }

            dataTable.Rows.Add(values);
        }

        return dataTable;
    }

and pass any type of object your LINQ query returning.

DataTable dt = CreateDataTableFromAnyCollection(query);

I hope this will help you.

Creating a DataTable From a Query (LINQ to DataSet)

要获取列名:

myTable.Columns.Cast<DataColumn>().Select(dc =>dc.ColumnName).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM