如何将字符串字段中的数据作为属性添加到模型中?
我通过拆分下面的字符串字段来分隔它。
细绳 : D837D323-40A0-1EDD-F365-A55C43725DE0|SPECODE,10A4F529-CDFB-7553-0D05-7C9A36A721F8|SPECODE2,BCAA7E77-DF1D-3135-AD4C-823CEC02ED56|SPECODE3, 24CC7FBE-5F43-67FC-7998-73869E2B6D5B|SPECODE4,DF15B062-3072-E3A3-A268-F2DEC8CF947B|SPECODE5,F56355C9-CCF6-095E-F697-DE36BAF464A7|CYPHCODE
拆分& UnitOfWork :
using (var dbFactory = new DbFactory()){
var specialCodeList = new List<SpecialCodeModel>();
string[] specialCode = _settings.SpecialCode.Split(',');
for (int i = 0; i < specialCode.Length; i++)
{
int position = specialCode[i].IndexOf('|');
specialCodeList.Add(new SpecialCodeModel
{
EkGuid = specialCode[i].Substring(0, position),
ErpProperty = specialCode[i].Substring(position + 1),
SystemTypeName = "System.String"
});
}
var factory = new DynamicTypeFactory();
var extendedType = factory.CreateNewTypeWithDynamicProperties(typeof(CustomerDto),
specialCodeList);
// Get all read/write properties for the extended Type.
var properties = extendedType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead && p.CanWrite);
var connection = dbFactory.GetConnection(_settings);
_settings = GetLogoFirmInfo(parameters, _settings, connection);
string query = SetLogoDbSettings(LogoSqlCommands.CustomerListCountCmd(), parameters, _settings);
int dataCount = connection.QueryFirstOrDefault<int>(query);
query = SetLogoDbSettings(LogoSqlCommands.CustomerListCmd(_settings), parameters, _settings);
var data = connection.Query<CustomerDto>(query);
return QueryResult<IEnumerable<CustomerDto>>.CreateResult(data, dataCount, parameters.PagingLimit, parameters.PagingOffset);
}
然后我将 SpecialCodeModel 分配给该模型。 我想要做的是将 EkGuid 字段作为 System.String 属性自动添加到 CustomerDto 模型中。然后我使用 dapper 和 ErpProperty 添加一个查询。以 EKGuid AS ErpProperty 形式形成的查询应该为我带来完整的 CustomerDto。当然,使用我专门添加的字段。 我尝试了一些东西,它添加到模型中,但短小精悍的人没有填充字段:(我需要这方面的帮助。
I am separating a string field I have below by splitting it.
String : D837D323-40A0-1EDD-F365-A55C43725DE0|SPECODE,10A4F529-CDFB-7553-0D05-7C9A36A721F8|SPECODE2,BCAA7E77-DF1D-3135-AD4C-823CEC02ED56|SPECODE3,24CC7FBE-5F43-67FC-7998-73869E2B6D5B|SPECODE4,DF15B062-3072-E3A3-A268-F2DEC8CF947B|SPECODE5,F56355C9-CCF6-095E-F697-DE36BAF464A7|CYPHCODE
Split & UnitOfWork :
using (var dbFactory = new DbFactory()){
var specialCodeList = new List<SpecialCodeModel>();
string[] specialCode = _settings.SpecialCode.Split(',');
for (int i = 0; i < specialCode.Length; i++)
{
int position = specialCode[i].IndexOf('|');
specialCodeList.Add(new SpecialCodeModel
{
EkGuid = specialCode[i].Substring(0, position),
ErpProperty = specialCode[i].Substring(position + 1),
SystemTypeName = "System.String"
});
}
var factory = new DynamicTypeFactory();
var extendedType = factory.CreateNewTypeWithDynamicProperties(typeof(CustomerDto),
specialCodeList);
// Get all read/write properties for the extended Type.
var properties = extendedType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead && p.CanWrite);
var connection = dbFactory.GetConnection(_settings);
_settings = GetLogoFirmInfo(parameters, _settings, connection);
string query = SetLogoDbSettings(LogoSqlCommands.CustomerListCountCmd(), parameters, _settings);
int dataCount = connection.QueryFirstOrDefault<int>(query);
query = SetLogoDbSettings(LogoSqlCommands.CustomerListCmd(_settings), parameters, _settings);
var data = connection.Query<CustomerDto>(query);
return QueryResult<IEnumerable<CustomerDto>>.CreateResult(data, dataCount, parameters.PagingLimit, parameters.PagingOffset);
}
Then I assign SpecialCodeModel to this model.
What I want to do is to automatically add the EkGuid fields to the CustomerDto model as a System.String property. Then I add a query using dapper with ErpProperty.The query formed as EKGuid AS ErpProperty should bring me a full CustomerDto.Of course, with the fields, I added specifically.
I tried something, it added to the model, but the dapper did not fill the fields :( I need help with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想法:使用一组可以由 Dapper 映射的固定别名来构造 SQL 命令:
现在,您可以创建一个具有一组固定属性的类
SQL 命令可以这样构造:
使用上面的字符串,它创建了这个输出:
确保以适合数据库类型的方式对列名进行转义(对于 SQL-Server 为
[]
,对于 Oracle 为""
等)。Idea: Construct a SQL command by using a fixed set of aliases that can be mapped by Dapper:
Now, you can create a class having a fixed set of properties
The SQL command can be constructed like this:
With the string from above it creates this output:
Make sure to escape the column names the appropriate way for the DB type (
[]
for SQL-Server,""
for Oracle, etc.).