开发者

Convert RowData into GridView Columns through LINQ

I am开发者_Python百科 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      jack@gmail.com

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

Username          Phone           Email
jack              2222222       jack@gmail.com

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?


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());
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜