tags:

views:

122

answers:

2

Is there any way to convert the result of a LINQ to DATATABlE without stepping through each element?

A: 

Nope there is no way to create it without stepping through each element. The Linq expression is evaluated when needed so it will step through each row (for matching and selection).

I think you should try using DataTable.Select() (MSDN link) method instead as it returns array of DataRow objects that you can add to new table as follows:

var rows = [ORIGINAL DATA TABLE].Select("id>5");

var dtb=[ORIGINAL DATA TABLE].Clone();

foreach(DataRow r in rows)
{
    var newRow = dtb.NewRow();
    newRow.ItemArray = r.ItemArray;
    dtb.Rows.Add(newRow);//I'm doubtful if you need to call this or not
}
TheVillageIdiot
I cannot use DataTable.Select() because iam using join to fetch records. anyway thnx and i will look forward to the traversing mechanism.
Kishore Kumar
+2  A: 

Credit to this blogger, but I've improved on his algorithm here. Make yourself an extension method:

    public static DataTable ToADOTable<T>(this IEnumerable<T> varlist)
    {
        DataTable dtReturn = new DataTable();
        // Use reflection to get property names, to create table
        // column names
        PropertyInfo[] oProps = typeof(T).GetProperties();
        foreach (PropertyInfo pi in oProps)
        {
            Type colType = pi.PropertyType; 
            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                colType = colType.GetGenericArguments()[0];
            dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
        }
        foreach (T rec in varlist)
        {
            DataRow dr = dtReturn.NewRow();
            foreach (PropertyInfo pi in oProps)
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
            dtReturn.Rows.Add(dr);
        }

        return (dtReturn);
    }

Usage:

DataTable dt = query.ToADOTable();
Shaul
I get this error when using your code: "CS0121: The call is ambiguous between the following methods or properties: <and then the class containing your method>" Do you know how to solve this? Thanks for code though, +1
Phil
@Phil - can't tell unless you post the full error text, which should contain a reference to both of the competing methods. Then you'll know which other method has the same signature.
Shaul
@Shaul - That's the thing.. it's the same signature for them both. Really strange! Anyway, here's the signature, some names in Swedish. If you don't know directly I'll just post a question about it. Error Message: CS0121: The call is ambiguous between the following methods or properties: 'Gruppkoll.App_Code.ConvertToDataTableFromLinqResult.ToADOTable<Gruppkoll.MessageSet>(System.Collections.Generic.IEnumerable<Gruppkoll.MessageSet>)' and 'Gruppkoll.App_Code.ConvertToDataTableFromLinqResult.ToADOTable<Gruppkoll.MessageSet>(System.Collections.Generic.IEnumerable<Gruppkoll.MessageSet>)'
Phil
What wrong could it do to post a question about it... http://stackoverflow.com/questions/4013057/error-on-ambigous-reference-points-to-same-method-twice
Phil