gpt4 book ai didi

c# - 如何实现一对多关系

转载 作者:IT王子 更新时间:2023-10-29 04:21:30 25 4
gpt4 key购买 nike

我有一个来自存储过程的一对多关系。我在查询中有几个一对多的关系,我试图将这些字段映射到 C# 对象。我遇到的问题是由于一对多关系,我得到了重复的数据。这是我的代码的简化版本:

这是对象类:

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public List<Color> FavoriteColors { get; set; }
public List<Hobby> Hobbies { get; set; }

public Person()
{
FavoriteColors = new List<Color>();
Hobbies = new List<Hobby>();
}
}

public class Color
{
public int Id { get; set; }
public string Name { get; set; }
}

public class Hobby
{
public int Id { get; set; }
public string Name { get; set; }
}

这是我检索数据的方式:

using (SqlConnection conn = new SqlConnection("connstring.."))
{
string sql = @"
SELECT
Person.Id AS PersonId,
Person.Name AS PersonName,
Hobby.Id AS HobbyId,
Hobby.Name AS HobbyName,
Color.Id AS ColorId,
Color.Name AS ColorName
FROM Person
INNER JOIN Color on Person.Id = Color.PersonId
INNER JOIN Hobby on Person.Id = Hobby.PersonId";
using (SqlCommand comm = new SqlCommand(sql, conn))
{
using (SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection))
{
List<Person> persons = new List<Person>();
while (reader.Read())
{
Person person = new Person();
//What to do
}
}
}
}

如您所见,给定的人可以有多种颜色和爱好。通常,我会使用 Entity Framework 来解决这个映射,但我们不允许使用任何 orms。是否有一种技术可以正确地展开这些数据?

最佳答案

这个想法是在对读者进行迭代时检查现有行人员 ID 是否存在于人员列表中。如果没有创建一个新的人对象并声明两个单独的列表来保存爱好和颜色信息。对于后续迭代,继续填充这两个列表,因为它们始终是相同的人员数据。一个你得到一个新人的新记录,将这些列表添加到人对象并从一个新的人对象开始

示例代码如下:

                string sql = @"
SELECT
Person.Id AS PersonId,
Person.Name AS PersonName,
Hobby.Id AS HobbyId,
Hobby.Name AS HobbyName,
Color.Id AS ColorId,
Color.Name AS ColorName
FROM Person
INNER JOIN Color on Person.Id = Color.PersonId
INNER JOIN Hobby on Person.Id = Hobby.PersonId
Order By PersonId"; // Order By is required to get the person data sorted as per the person id
using (SqlCommand comm = new SqlCommand(sql, conn))
{
using (SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection))
{
List<Person> persons = new List<Person>();
while (reader.Read())
{
var personId = reader.GetInt32(0);
var personName = reader.GetString(1);
var hobbyId = reader.GetInt32(3);
var hobbyName = reader.GetString(4);
var colorId = reader.GetInt32(5);
var colorName = reader.GetString(6);

var person = persons.Where(p => p.Id == personId).FirstOrDefault();
if (person == null)
{
person = new Person();
person.Id = personId;
person.Name = personName;

hobby = new Hobby() { Id = hobbyId, Name = hobbyName };
color = new Color() { Id = colorId, Name = colorName };

person.FavoriteColors = new List<Color>();
person.Hobbies = new List<Hobby>();

person.FavoriteColors.Add(color);
person.Hobbies.Add(hobby);

persons.Add(person);
}
else
{
hobby = new Hobby() { Id = hobbyId, Name = hobbyName };
color = new Color() { Id = colorId, Name = colorName };

//JT Edit: if the colour/hobby doesn't already exists then add it
if (!person.FavoriteColors.Contains(color))
person.FavoriteColors.Add(color);

if (!person.Hobbies.Contains(hobby))
person.Hobbies.Add(hobby);
}
}
}
}
}

关于c# - 如何实现一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27371218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com