views:

50

answers:

1

Hi Everyboyd

I am using Linq to Sql. When My StoredProcedure Executing its result ll returned in form of IMultipleResults.

I am converting it to ISingleResults and It can be Casted as List.

So I need to Convert it to DataTable so That I can Bind it to Dataset and Pass the values to UI.

But I want such method where I can convert it without LOOP.

Please Any body Help me.

For Any Clarification Contact me.

Thanking you.

A: 

First you might want to see if you could cast your list to a BindingList, or do something like this:

BindingListX = New BindingList(Of elementX)(QueryResult.ToList())

You might be able to bind with that. If that doesn't work though here's another question: Do you want to not use a loop in your code at all, or just not see a loop in your code? It is possible to create an extension function that does the conversion:

public static class HelperFunctions
    {
        public static void Map<T>(this IEnumerable<T> source, Action<T> func)
        {
            foreach (T i in source)
                func(i);
        }

        public static DataTable ToDataTable<T>(this IEnumerable<T> source)
        {
            var dt = new DataTable();
            var properties = typeof(T).GetProperties();
            dt.Columns.AddRange(properties.Select(x => new DataColumn(x.Name, x.PropertyType)).ToArray());
            source.Select(x => dt.NewRow().ItemArray = properties.Select(y => y.GetValue(x, null)).ToArray()).Map(x => dt.Rows.Add(x));
            return dt;
        }
    }

But as you can see, the other extension function 'Map' is nothing more than a foreach that can be used inline. If that is not what you want then I apologize and will try to come up with a different way.

It is also worth noting that I'm not sure if assigning to Item Array is the best way to populate a row so if it doesn't work then please let me know so that I may draft another version of the function.

Hope this helps!

diceguyd30
Sir I am not getting code can you plz make it simple and I dont want to use any loop in my code so plz try to do it.
Umakanta.Swain
So casting it as a 'BindingList' didn't work?
diceguyd30