我的表单有两个模型,一个用于它的 ViewModel 和一个来自它的 ControlModel。 ControlModel 具有所有相同的字段名称和层次结构,但所有字段都是字符串数据类型。
您将如何编写 AutoMapper 代码以将字符串字段转换为整数?我尝试了 Int32.Parse(myString) 但 Int32 在表达式中不可用(给出错误)。
Mapper.CreateMap<SourceClass, DestinationClass>()
.ForMember(dest => dest.myInteger,
opt => opt.MapFrom(src => src.myString));
类中的类型及其对应的转换类型:
字符串到 int、int?、double、double?、DateTime 和 bool
此外,是否有任何方法可以通过使用该函数解析目标中的所有整数来概括映射?换句话说,有没有办法为数据类型创建映射?
编辑:
这看起来很有希望:
AutoMapper.Mapper.CreateMap<string, int>()
.ConvertUsing(src => Convert.ToInt32(src));
编辑: 这post真的很有帮助
最佳答案
我最终做了这样的事情:
Mapper.CreateMap<string, int>().ConvertUsing<IntTypeConverter>();
Mapper.CreateMap<string, int?>().ConvertUsing<NullIntTypeConverter>();
Mapper.CreateMap<string, decimal?>().ConvertUsing<NullDecimalTypeConverter>();
Mapper.CreateMap<string, decimal>().ConvertUsing<DecimalTypeConverter>();
Mapper.CreateMap<string, bool?>().ConvertUsing<NullBooleanTypeConverter>();
Mapper.CreateMap<string, bool>().ConvertUsing<BooleanTypeConverter>();
Mapper.CreateMap<string, Int64?>().ConvertUsing<NullInt64TypeConverter>();
Mapper.CreateMap<string, Int64>().ConvertUsing<Int64TypeConverter>();
Mapper.CreateMap<string, DateTime?>().ConvertUsing<NullDateTimeTypeConverter>();
Mapper.CreateMap<string, DateTime>().ConvertUsing<DateTimeTypeConverter>();
Mapper.CreateMap<SourceClass, DestClass>();
Mapper.Map(mySourceObject, myDestinationObject);
及其引用的类(初稿):
// TODO: Boil down to two with Generics if possible
#region AutoMapTypeConverters
// Automap type converter definitions for
// int, int?, decimal, decimal?, bool, bool?, Int64, Int64?, DateTime
// Automapper string to int?
private class NullIntTypeConverter : TypeConverter<string, int?>
{ protected override int? ConvertCore(string source)
{ if (source == null)
return null;
else
{ int result;
return Int32.TryParse(source, out result) ? (int?) result : null;
} } }
// Automapper string to int
private class IntTypeConverter : TypeConverter<string, int>
{ protected override int ConvertCore(string source)
{ if (source == null)
throw new MappingException("null string value cannot convert to non-nullable return type.");
else
return Int32.Parse(source);
} }
// Automapper string to decimal?
private class NullDecimalTypeConverter : TypeConverter<string, decimal?>
{ protected override decimal? ConvertCore(string source)
{ if (source == null)
return null;
else
{ decimal result;
return Decimal.TryParse(source, out result) ? (decimal?) result : null;
} } }
// Automapper string to decimal
private class DecimalTypeConverter : TypeConverter<string, decimal>
{ protected override decimal ConvertCore(string source)
{ if (source == null)
throw new MappingException("null string value cannot convert to non-nullable return type.");
else
return Decimal.Parse(source);
} }
// Automapper string to bool?
private class NullBooleanTypeConverter : TypeConverter<string, bool?>
{ protected override bool? ConvertCore(string source)
{ if (source == null)
return null;
else
{ bool result;
return Boolean.TryParse(source, out result) ? (bool?) result : null;
} } }
// Automapper string to bool
private class BooleanTypeConverter : TypeConverter<string, bool>
{ protected override bool ConvertCore(string source)
{ if (source == null)
throw new MappingException("null string value cannot convert to non-nullable return type.");
else
return Boolean.Parse(source);
} }
// Automapper string to Int64?
private class NullInt64TypeConverter : TypeConverter<string, Int64?>
{ protected override Int64? ConvertCore(string source)
{ if (source == null)
return null;
else
{ Int64 result;
return Int64.TryParse(source, out result) ? (Int64?)result : null;
} } }
// Automapper string to Int64
private class Int64TypeConverter : TypeConverter<string, Int64>
{ protected override Int64 ConvertCore(string source)
{ if (source == null)
throw new MappingException("null string value cannot convert to non-nullable return type.");
else
return Int64.Parse(source);
} }
// Automapper string to DateTime?
// In our case, the datetime will be a JSON2.org datetime
// Example: "/Date(1288296203190)/"
private class NullDateTimeTypeConverter : TypeConverter<string, DateTime?>
{ protected override DateTime? ConvertCore(string source)
{ if (source == null)
return null;
else
{ DateTime result;
return DateTime.TryParse(source, out result) ? (DateTime?) result : null;
} } }
// Automapper string to DateTime
private class DateTimeTypeConverter : TypeConverter<string, DateTime>
{ protected override DateTime ConvertCore(string source)
{ if (source == null)
throw new MappingException("null string value cannot convert to non-nullable return type.");
else
return DateTime.Parse(source);
} }
#endregion
关于c# - AutoMapper:如何从 String 解析 Int 并可能根据数据类型创建规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4101516/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i