大神求救啊,query查询数据库数据之后,使用todictionary的方法

weixin_38328143 2017-07-15 12:00:42
大神求救啊,query查询数据库数据之后,使用todictionary的方法存储数据
namespace TestDictionary1
{
class UsersClass
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string StudentId { get; set; }
public DateTime Time { get; set; }
}
}
}
var result= Conn.Query<UsersClass.User>("select * from Students").ToDictionary(i=>i.Id,i=>i.Name,i=>i.StudentId,i=>i.Time);
然后再遍历result输出数据,这样运行错误
...全文
280 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38328143 2017-07-16
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
参见http://m.blog.csdn.net/cyb331/article/details/16939927
就是怎么样根据query之后,映射到Student类的数据,转换成dictionary,因为接下来要输出为CSV文件
weixin_38328143 2017-07-16
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
参见http://m.blog.csdn.net/cyb331/article/details/16939927
class Program { public class User { public int Id { get; set; } public string Name { get; set; } public string StudentId { get; set; } public DateTime Time { get; set; } } static string ConnectString = "Data Source=192.168.0.100;Initial Catalog=Users;User ID=sa;Password=0902"; static void Main(string[] args) { IDbConnection Conn = new SqlConnection(ConnectString); Conn.Open(); Console.WriteLine("Open DataBase"); List<User> asd= Conn.Query<User>("select * from Students").ToList();//下一步是怎么样把list转换成dictionar Dictionary<int, string > mm = asd.ToDictionary(i => i.Id, i => i.Name+","+i.StudentId+","+i.Time); foreach (KeyValuePair<int, string > cc in mm) { Console.WriteLine(cc.Key+","+cc.Value); } 以上为源码,现在这种方法是可以,但是我想Value可以自动获取到Name,StudentId之类的属性,而不是自己输入进去,求大神赐教。
weixin_38328143 2017-07-15
  • 打赏
  • 举报
回复
本人新手,希望各位多多指教
exception92 2017-07-15
  • 打赏
  • 举报
回复
参见http://m.blog.csdn.net/cyb331/article/details/16939927
exception92 2017-07-15
  • 打赏
  • 举报
回复
目测ToDictionary 只能包含一个key,一个value,但是楼主查询出不止两个
exception92 2017-07-15
  • 打赏
  • 举报
回复
最起码得什么错贴出来啊

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧