本文介绍了小巧玲珑。映射到SQL列与列名的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我已经成功地得到的东西和运行今天的小沙盒/ POC项目,但似乎撞我的头一个问题... 问: 有没有办法让短小精悍映射到带空格的SQL列名 我有事要这个效果我的结果集。 例如: SELECT 001 AS [西1], 901 [西2] 00454345345345435349 AS [西3], 03453453453454353458 AS [COL 4] 从[一些架构] [有些表] 和我的课是这样 公共类ClassA的 {公共字符串COL1 {搞定;组; } 公共字符串col2的{搞定;组; } /// ...等} 我的实现看起来像这样的时刻 公开元组LT; IList的< TClass>中的IList< TClass2>> QueryMultiple< TClass,TClass2>(对象参数) {名单,LT; TClass> OUTPUT1; 名单,LT; TClass2>输出2; TClass>使用(VAR数据= this.Connection.QueryMultiple(this.GlobalParameter.RpcProcedureName,参数,CommandType.StoredProcedure)) {输出1 = data.Read< ( ).ToList(); 输出2 = data.Read&所述; TClass2>()了ToList(); } VAR的结果=新的记录<&IList的LT; TClass>中的IList< TClass2>>(输出1,输出2); 返回结果; } 注:SQL不能以任何方式修改 目前我打算通过短小精悍的代码,我唯一可以预见的解决办法是添加一些代码来说服栏目比较,但没有多少运气至今。 我已经看到了计算器,有喜欢的东西短小精悍的扩展,但我希望我能不增加的延伸,这个工作如果不。我会采取什么是最快的实现。 解决方案 这里的一个办法是通过动态/非通用API去字符串,对象> 行的API,但可能是有点乏味 $ b,然后通过的IDictionary<取值了$ b 作为一种替代方法,您可以创建自定义映射器,并告诉它短小精悍;例如: SqlMapper.SetTypeMap(typeof运算(ClassA的),新RemoveSpacesMap()); 与 类RemoveSpacesMap:Dapper.SqlMapper.ITypeMap { System.Reflection.ConstructorInfo SqlMapper.ITypeMap.FindConstructor(字符串[]的名称,类型[]类型) {返回NULL; } SqlMapper.IMemberMap SqlMapper.ITypeMap.GetConstructorParameter(System.Reflection.ConstructorInfo构造,串COLUMNNAME) {返回NULL; } SqlMapper.IMemberMap SqlMapper.ITypeMap.GetMember(字符串COLUMNNAME) { VAR道具= typeof运算(ClassA的).GetProperty(columnName.Replace( ,)); 返回道具== NULL?空:新PropertyMemberMap(COLUMNNAME,道具); } 类PropertyMemberMap:Dapper.SqlMapper.IMemberMap {私人字符串COLUMNNAME; 私人的PropertyInfo财产; 公共PropertyMemberMap(字符串COLUMNNAME,财产的PropertyInfo) { this.columnName = COLUMNNAME; this.property =财产; } 串SqlMapper.IMemberMap.ColumnName {获得{抛出新NotImplementedException(); } } System.Reflection.FieldInfo SqlMapper.IMemberMap.Field { {返回NULL; } } 型SqlMapper.IMemberMap.MemberType { {返回property.PropertyType; } } System.Reflection.ParameterInfo SqlMapper.IMemberMap.Parameter { {返回NULL; } } System.Reflection.PropertyInfo SqlMapper.IMemberMap.Property { {返回属性; } } } } I've managed to get something up and running today as small sandbox/POC project, but have seemed to bump my head on one issue...Question: Is there a way to get dapper to map to SQL column names with spaces in them.I have something to this effect as my result set.For example: SELECT 001 AS [Col 1], 901 AS [Col 2], 00454345345345435349 AS [Col 3], 03453453453454353458 AS [Col 4]FROM [Some Schema].[Some Table]And my class would look like this public class ClassA { public string Col1 { get; set; } public string Col2 { get; set; } ///... etc }My implementation looks like this at the moment public Tuple<IList<TClass>, IList<TClass2>> QueryMultiple<TClass, TClass2>(object parameters) { List<TClass> output1; List<TClass2> output2; using (var data = this.Connection.QueryMultiple(this.GlobalParameter.RpcProcedureName, parameters, CommandType.StoredProcedure)) { output1 = data.Read<TClass>().ToList(); output2 = data.Read<TClass2>().ToList(); } var result = new Tuple<IList<TClass>, IList<TClass2>>(output1, output2); return result; }Note: The SQL cant be modified in any way.Currently I'm going through the dapper code, and my only foreseeable solution is to add some code to "persuade" the column comparison, but not having much luck so far.I've seen on StackOverflow that there are things like dapper extensions, but I'm hoping I can get this done without adding an extention, if not. I'll take whatever is quickest to implement. 解决方案 One option here would be to go via the dynamic / non-generic API, and then fetch the values out via the IDictionary<string,object> API per row, but that might be a bit tedious.As an alternative, you can create a custom mapper, and tell dapper about it; for example:SqlMapper.SetTypeMap(typeof(ClassA), new RemoveSpacesMap());with:class RemoveSpacesMap : Dapper.SqlMapper.ITypeMap{ System.Reflection.ConstructorInfo SqlMapper.ITypeMap.FindConstructor(string[] names, Type[] types) { return null; } SqlMapper.IMemberMap SqlMapper.ITypeMap.GetConstructorParameter(System.Reflection.ConstructorInfo constructor, string columnName) { return null; } SqlMapper.IMemberMap SqlMapper.ITypeMap.GetMember(string columnName) { var prop = typeof(ClassA).GetProperty(columnName.Replace(" ", "")); return prop == null ? null : new PropertyMemberMap(columnName, prop); } class PropertyMemberMap : Dapper.SqlMapper.IMemberMap { private string columnName; private PropertyInfo property; public PropertyMemberMap(string columnName, PropertyInfo property) { this.columnName = columnName; this.property = property; } string SqlMapper.IMemberMap.ColumnName { get { throw new NotImplementedException(); } } System.Reflection.FieldInfo SqlMapper.IMemberMap.Field { get { return null; } } Type SqlMapper.IMemberMap.MemberType { get { return property.PropertyType; } } System.Reflection.ParameterInfo SqlMapper.IMemberMap.Parameter { get { return null; } } System.Reflection.PropertyInfo SqlMapper.IMemberMap.Property { get { return property; } } }} 这篇关于小巧玲珑。映射到SQL列与列名的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 16:57