views:

3834

answers:

2

My query is:

var query1 = from u in dc.Usage_Computers.AsEnumerable
             where u.DomainUser == s3
             orderby u.OperationTime descending
             select new
             {
                 u.ProgramVersion,
                 u.OperationTime,
                 u.IPaddress,
                 u.ComputerName,
                 u.DomainUser,
                 u.OnNetwork,
                 Operation = u.Operation == 1 ? "Login" :
                             u.Operation == 2 ? "Logoff" :
                             u.Operation == 3 ? "AGNS Connect" :
                             u.Operation == 4 ? "AGNS Disconnect" :
                             "None"
             };

GridView1.DataSource = query1;
GridView1.DataBind();

After databinding with gridview I wanted to add the result set "query1" into dataset or datatable.Can anyone give me any idea how to do it?

I saw another post here with same problem but that answer did not work in mine...

*Note: I am using VS 2008 *

+5  A: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

public static class IEnumerableExt
{
    public static DataTable ToDataTable<T>(this IEnumerable<T> things) where T : class
    {
        DataTable tbl = new System.Data.DataTable();
        bool buildColumns = false;
        foreach (var item in things)
        {
            Type t = item.GetType();
            var properties = t.GetProperties();
            if (!buildColumns)
            {
                foreach (var prop in properties)
                {
                    Type ptype = prop.PropertyType;
                    if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        ptype = Nullable.GetUnderlyingType(prop.PropertyType.UnderlyingSystemType);
                    }
                    DataColumn col = new DataColumn(prop.Name, ptype);
                    tbl.Columns.Add(col);
                }
                buildColumns = true;
            }
            DataRow row = tbl.NewRow();

            foreach (var prop in properties)
            {
                if (prop.GetValue(item, null) == null)
                {
                    row[prop.Name] = DBNull.Value;
                }
                else
                {
                    row[prop.Name] = prop.GetValue(item, null);
                }
            }

            tbl.Rows.Add(row);
        }

        return tbl;
    }
}

there is no CopyToDataTable unless your dealing with DataRows look at these:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2162392&amp;SiteID=1

http://oakleafblog.blogspot.com/2007/03/linq-missing-todatatable-method-saga.html

good alternative alot more involved than my code: http://blogs.msdn.com/aconrad/archive/2007/09/07/science-project.aspx

Edit: Updated so it works

Hath
This code is not working for me!
changed some things so it should work now
Hath
A: 

Unfortunately Your code is not working for me!!! I getting thw following error message:

Error System.NotSupportedException: DataSet does not support System.Nullable<>. at System.Data.DataColumn..ctor(String columnName, Type dataType, String expr, MappingType type) at System.Data.DataColumn..ctor(String columnName, Type dataType) at ComputerAnalyzer.Graph.Gridquery() in Z:\Apps\ComputerAnalyzer\Graph.aspx.cs:line 145

My code is here:

                     var query1 = from u in dc.Usage_Computers
                                 where u.DomainUser == s3
                                 orderby u.OperationTime descending
                                 select new
                                 {
                                     u.ProgramVersion,
                                     u.OperationTime,
                                     u.IPaddress,
                                     u.ComputerName,
                                     u.DomainUser,
                                     u.OnNetwork,
                                     Operation = u.Operation == 1 ? "Login" :
                                                 u.Operation == 2 ? "Logoff" :
                                                 u.Operation == 3 ? "AGNS Connect" :
                                                 u.Operation == 4 ? "AGNS Disconnect" :
                                                 "None"
                                 };

                    GridView1.DataSource = query1;
                    GridView1.DataBind();


                    System.Data.DataTable tbl = new System.Data.DataTable();

                    bool buildColumns = false;
                    foreach (var item in query1)
                    {
                        Type t = item.GetType();
                        var properties = t.GetProperties();
                        if (!buildColumns)
                        {
                            foreach (var prop in properties)
                            {
                                System.Data.DataColumn col = new System.Data.DataColumn(prop.Name, prop.PropertyType);
                                tbl.Columns.Add(col);
                            }
                            buildColumns = true;
                        }
                        System.Data.DataRow row = tbl.NewRow();

                        foreach (var prop in properties)
                        {
                            row[prop.Name] = prop.GetValue(item, null);
                        }

                        tbl.Rows.Add(row);
                    }
                    Session["dt"] = tbl;
i've added an edit to my code that checks for that type.
Hath