如何将字符串字段中的数据作为属性添加到模型中?

发布于 2025-01-11 21:03:57 字数 2163 浏览 0 评论 0原文

我通过拆分下面的字符串字段来分隔它。

细绳 : 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2025-01-18 21:03:58

想法:使用一组可以由 Dapper 映射的固定别名来构造 SQL 命令:

SELECT
    [D837D323-40A0-1EDD-F365-A55C43725DE0] AS Column1,
    [10A4F529-CDFB-7553-0D05-7C9A36A721F8] AS Column2
    ...
FROM mytable

现在,您可以创建一个具有一组固定属性的类

public class Data
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    ...
}

SQL 命令可以这样构造:

var specialCodes = _settings.SpecialCode.Split(',')
    .Select(s => {
        string[] parts = s.Split('|');
        return new SpecialCodeModel {
            EkGuid = parts[0],
            ErpProperty = parts[1],
            SystemTypeName = "System.String"
        };
    });


var selectList = specialCodes
    .Select((s, i) => $"[{s.EkGuid}] AS Column{i + 1}");

string sql = $"SELECT\r\n  {String.Join(",\r\n  ", selectList)}\r\nFROM myTable";

Console.WriteLine(sql);

使用上面的字符串,它创建了这个输出:

SELECT
  [D837D323-40A0-1EDD-F365-A55C43725DE0] AS Column1,
  [10A4F529-CDFB-7553-0D05-7C9A36A721F8] AS Column2,
  [BCAA7E77-DF1D-3135-AD4C-823CEC02ED56] AS Column3,
  [24CC7FBE-5F43-67FC-7998-73869E2B6D5B] AS Column4,
  [DF15B062-3072-E3A3-A268-F2DEC8CF947B] AS Column5,
  [F56355C9-CCF6-095E-F697-DE36BAF464A7] AS Column6
FROM myTable

确保以适合数据库类型的方式对列名进行转义(对于 SQL-Server 为 [],对于 Oracle 为 "" 等)。

Idea: Construct a SQL command by using a fixed set of aliases that can be mapped by Dapper:

SELECT
    [D837D323-40A0-1EDD-F365-A55C43725DE0] AS Column1,
    [10A4F529-CDFB-7553-0D05-7C9A36A721F8] AS Column2
    ...
FROM mytable

Now, you can create a class having a fixed set of properties

public class Data
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    ...
}

The SQL command can be constructed like this:

var specialCodes = _settings.SpecialCode.Split(',')
    .Select(s => {
        string[] parts = s.Split('|');
        return new SpecialCodeModel {
            EkGuid = parts[0],
            ErpProperty = parts[1],
            SystemTypeName = "System.String"
        };
    });


var selectList = specialCodes
    .Select((s, i) => 
quot;[{s.EkGuid}] AS Column{i + 1}");

string sql = 
quot;SELECT\r\n  {String.Join(",\r\n  ", selectList)}\r\nFROM myTable";

Console.WriteLine(sql);

With the string from above it creates this output:

SELECT
  [D837D323-40A0-1EDD-F365-A55C43725DE0] AS Column1,
  [10A4F529-CDFB-7553-0D05-7C9A36A721F8] AS Column2,
  [BCAA7E77-DF1D-3135-AD4C-823CEC02ED56] AS Column3,
  [24CC7FBE-5F43-67FC-7998-73869E2B6D5B] AS Column4,
  [DF15B062-3072-E3A3-A268-F2DEC8CF947B] AS Column5,
  [F56355C9-CCF6-095E-F697-DE36BAF464A7] AS Column6
FROM myTable

Make sure to escape the column names the appropriate way for the DB type ([] for SQL-Server, "" for Oracle, etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文