我有一个List,其中包含以下模板中的记录。
日期| 用户| 问题| 答(可选)
对于哪些记录将是
02/02/2015 | 哈利| 什么是..? | 五
02/02/2015 | 哈利| 怎么样..? | 4
02/03/2015 | 爱丽丝| 什么是..? | 3
02/03/2015 | 爱丽丝| 怎么样..? | 1
我需要一个数据Gridview来显示以下模板中的记录。
日期| 用户| 什么是..? | 怎么样..?
02/02/2015 | 哈利| 5 | 4
02/03/2015 | 爱丽丝| 3 | 1
谁能帮我吗。 有没有聪明的方法来做到这一点? 提前致谢。
I have a List which contains records in the following template.
Date | User | Question | Answer(Option)
For which the records will be like
02/02/2015 | Harry | What is..? | 5
02/02/2015 | Harry | How..? | 4
02/03/2015 | Alice | What is..? | 3
02/03/2015 | Alice | How..? | 1
I need a data Gridview to show records in following template.
Date | User | What is..? | How..?
02/02/2015 | Harry | 5 | 4
02/03/2015 | Alice | 3 | 1
Can anyone help me out. Is there any smart way to do this? Thanks in advance.
最满意答案
不确定这是否是预期的答案,但它会产生预期的结果。
List<Record> Records = new List<Record>(); Records.Add(new Record { Answer = 5, Date = DateTime.Parse("02/02/2015"), User = "Harry", Question = "What is..?" }); Records.Add(new Record { Answer = 4, Date = DateTime.Parse("02/02/2015"), User = "Harry", Question = "How is..?" }); Records.Add(new Record { Answer = 3, Date = DateTime.Parse("02/03/2015"), User = "Alice", Question = "What is..?" }); Records.Add(new Record { Answer = 1, Date = DateTime.Parse("02/03/2015"), User = "Alice", Question = "How is..?" }); var Result = Records.GroupBy(r => new { r.Date, r.User }) .Select(g => new { Date = g.Key.Date, User = g.Key.User, WhatIs = g.Where(i => i.Question == "What is..?") .Where(i => i.User == g.Key.User).Select(i => i.Answer).First(), HowIs = g.Where(i => i.Question == "How is..?") .Where(i => i.User == g.Key.User).Select(i => i.Answer).First() }).ToList(); dataGridView1.DataSource = Result;Not sure if that's the kind of answer expected, but it produces the expected result.
List<Record> Records = new List<Record>(); Records.Add(new Record { Answer = 5, Date = DateTime.Parse("02/02/2015"), User = "Harry", Question = "What is..?" }); Records.Add(new Record { Answer = 4, Date = DateTime.Parse("02/02/2015"), User = "Harry", Question = "How is..?" }); Records.Add(new Record { Answer = 3, Date = DateTime.Parse("02/03/2015"), User = "Alice", Question = "What is..?" }); Records.Add(new Record { Answer = 1, Date = DateTime.Parse("02/03/2015"), User = "Alice", Question = "How is..?" }); var Result = Records.GroupBy(r => new { r.Date, r.User }) .Select(g => new { Date = g.Key.Date, User = g.Key.User, WhatIs = g.Where(i => i.Question == "What is..?") .Where(i => i.User == g.Key.User).Select(i => i.Answer).First(), HowIs = g.Where(i => i.Question == "How is..?") .Where(i => i.User == g.Key.User).Select(i => i.Answer).First() }).ToList(); dataGridView1.DataSource = Result;更多推荐
records,Gridview,爱丽丝,电脑培训,计算机培训,IT培训"/> <meta name="descript
发布评论