【问题标题】:Convert DataReader to C# Object将 DataReader 转换为 C# 对象
【发布时间】:2020-12-02 21:04:20
【问题描述】:

我有课

public class Person
{
 public string Name {get; set;}
 public List<string> Friends {get; set;}
}

我有一个从数据库中获取这些值的数据读取器,我想使用 DataReader 填充上述类。

通常细节如下所示

Name    Friends
John    Caleb
John    Matthew
John    Simon
Andrew  Bolton
Andrew  Mark

我需要根据姓名对其进行分组,并为每个姓名填写朋友列表。

有人可以帮忙吗?我无法获得分组的朋友列表。什么是实现这个逻辑的正确代码。 谢谢

【问题讨论】:

  • 请不要改变你问题的核心,否则没有人愿意再回答它了......
  • 对此感到抱歉。实际上我无法读取数据读取器并获取值。
  • 不,我没有使用 Dapper
  • 我建议阅读 Dapper。
  • 基本上,您将在 dataReader while 循环中填充List&lt;person&gt;,在其中您可以按姓名检查现有人员,如果存在则仅将朋友添加到friends 列表中,如果然后不添加新人

标签: c# datatable data-access-layer datamapper sqldatareader


【解决方案1】:

我假设你的表喜欢 (姓名,朋友)

你必须阅读你的友谊表

那么你必须将它们分组 喜欢

var groupedTempList = tempList.GroupBy(a=>a.Name)

然后

List<Person> t = new List<Person>();

foreach(var Item in groupedTempList) 
{ 
    Person tP = new Person() {Name = Item.Key};
    foreach(YourTableEquivalentClass subitem in Item)
    {
        tP.Friends.Add(subitem.Friend);
    }
}

这里是 dbreader 的扩展:

    public static int? GetNullableInteger(this SqlDataReader dbReader, string name)
    {
        var tVal = dbReader[name];
        if (Convert.IsDBNull(tVal)) return null;
        return new int?(int.Parse(tVal.ToString()));
    }

    public static int GetInteger(this SqlDataReader dbReader, string name)
    {
        int? tVal = dbReader.GetNullableInteger(name);
        if (tVal == null) throw new ArgumentNullException("Der abgerufene int/int ist null/DbNull!");
        return (int)tVal;
    }


    public static string GetString(this SqlDataReader dbReader, string name)
    {
        var tVal = dbReader[name];
        if (Convert.IsDBNull(tVal)) return null;
        return tVal.ToString();
    }

阅读资料

                List<YourTableEquivalentClass> temp = new List<YourTableEquivalentClass>();
                string sqlComStr =
                      @"SELECT [Name]
                              ,[Friend]
                          FROM [YourDadabase].[dbo].[YourFriendshipTable]";
                using (SqlConnection con = new SqlConnection(YourConnectionString))
                {
                    con.Open();
                    using (SqlCommand sqlCom = new SqlCommand(sqlComStr, con))
                    {
                        using (SqlDataReader dbReader = sqlCom.ExecuteReader())
                        {
                            if (dbReader.HasRows)
                            {
                                while (dbReader.Read())
                                {
                                    temp.Add(new YourTableEquivalentClass()
                                    {
                                        Name = dbReader.GetString("Name"),
                                        Friend = dbReader.GetString("Friend")
                                    });
                                }
                            }
                        }
                    }
                }

【讨论】:

  • 是的,也许 1. 和 2. 代码部分有一个更短的 LINQ 表达式。
猜你喜欢
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 2015-07-20
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多