简体   繁体   English

逗号分隔在字符串C#和dapper的列表中

[英]Comma seperated in list of strings c# and dapper

I have a class which has this property among others: 我有一个具有此属性的班级:

public class ADUser
{
    public IList<string> Relations { get; set; }
}

In my database table, Relations is a comma-separated string column. 在我的数据库表中, Relations是一个用逗号分隔的字符串列。

I am using Dapper to get data. 我正在使用Dapper来获取数据。

This is my code: 这是我的代码:

public ADUser[] GetWithGroupsAndRelations()
{
        using (var connection = ConnectionFactory.CreateConnection())
        {
            int numberOfUsersProcessed = 0;
            _log.LogInfo("Getting users from database...");

            var users = connection.GetList<ADUser>().ToArray();
            //var users = connection.GetList<ADUser>().Take(1000).OrderBy(x => x.Id).ToArray();

            _log.LogInfo($"Done getting {users.Count()} users from database...");

            return users;
        }
}

How can I map the comma-separated Relations column to a list of strings while reading the data? 在读取数据时,如何将逗号分隔的“ Relations列映射到字符串列表?

Thanks. 谢谢。 :) :)

You should be able to do this without using any extensions (untested!): 您应该能够在不使用任何扩展名的情况下做到这一点(未经测试!):

ADUser[] users = connection.Query("SELECT Relations FROM AdUser")
    .Select(x => new ADUser() { Relations = ((string)x.Relations)?.Split(new char[1] { ',' })?.ToList() })
    .ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM