tags:

views:

113

answers:

2

I am currently working on a project where i need to show row data in gridview columns, so for example, if my data is in the following form.

Username   Jack
Phone      2222222
Email      [email protected]

and i want to show this data as per columns in gridview as below

Username          Phone           Email
jack              2222222       [email protected]

How would i do it? specially through LINQ, like can i query DB in any way so it return data in the form that i want?

A: 

if you are showing the date in the asp:GRIDVIEW, you don't need to do anything special

just do a simple:

var data  = (from t in db.YourTable select t).AsEnumerable();

and then assign data as DataSource of your GridView.

roman m
A: 

You want to Transpose your data. Here's an extension method you can use, shamelessly stolen from here:

public static IEnumerable<IEnumerable<T>> Transpose<T>(this IEnumerable<IEnumerable<T>> values)
    {
        if (values.Count() == 0) 
            return values;
        if (values.First().Count() == 0) 
            return Transpose(values.Skip(1));

        var x = values.First().First();
        var xs = values.First().Skip(1);
        var xss = values.Skip(1);
        return
         new[] {new[] {x}
           .Concat(xss.Select(ht => ht.First()))}
           .Concat(new[] { xs }
           .Concat(xss.Select(ht => ht.Skip(1)))
           .Transpose());
    }
Winston Smith