开发者

Convert RowData into GridView Columns through LINQ

开发者 https://www.devze.com 2022-12-20 10:48 出处:网络
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.

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

精彩评论

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

关注公众号