c#如何使用sql填充包含列表的复杂对象

时间:2014-03-31 08:14:30

标签: c# sql list object join

我目前有一个使用Entity Framework的对象模型,它包含一个复杂的对象,其属性是一个列表。我将它存储在数据库中,并在此处提出第二个选项:Representing a list in a SQL database

但总结一下:

Person()
{
  long Id;//primary key
  string Name;
  List<long> ResponsibleFor; //list of id    
}

存储为下面的2个表

Persons:
 Id,
 Name

Responsibilities:
 Id,
 PersonId,
 ResponsibleForId

现在我对如何重新填充Person对象感到困惑。我目前的方法如下:

 from p in db.Persons
 join r in db.Responsibilities on p.Id equals r.PersonId
 where p.Id == IdImSearchingFor
 select new Person
 {
  Id = p.Id
  ResponsibileFor = ?
 }

我一直试图重新填充List属性。

任何建议?

谢谢!

5 个答案:

答案 0 :(得分:1)

您需要对联接的结果进行分组,以便让所有人负责。

尝试

var result = from p in db.Persons
             join r in db.Responsibilities on p.Id equals r.PersonId
             where p.Id == IdImSearchingFor
             group r.ResponsibleForId by p into g
             select new Person 
             {
                 Id = g.Key.Id, 
                 Name = g.Key.Name,
                 ResponsibleFor = g.ToList()
             };

答案 1 :(得分:0)

假设您想要获得Id 1的人...

from p in db.Persons
 join r in db.Responsibilities on p.Id equals r.PersonId
 where p.Id == 1
 select new Person
 {
  Id = p.Id
  ResponsibileFor = p.ReponsibleFor 
}

答案 2 :(得分:0)

检查一下。我已经测试过普通类和列表,而不是EF或任何其他ORM。

以下是我理解的类结构

class Person
{
    public long Id;//primary key
    public string Name;
}

class Responsibility
{
    public long Id;//primary key
    public long PersonId;
    public long ResponsibleForId; 
}

以下是2个测试列表

List<Person> Persons = new List<Person>() { new Person() { Id = 1, Name = "Samar" } };
List<Responsibility> Responsibilities = new List<Responsibility>() { new Responsibility() { Id = 1, PersonId = 1, ResponsibleForId = 1 }, new Responsibility() { Id = 2, PersonId = 1, ResponsibleForId = 2 } };

以下是查询

var allPeople = from p in Persons
                join r in Responsibilities on p.Id equals r.PersonId into g
                where p.Id == 1
                select new
                {
                    Id = p.Id,
                    ReponsibleFor = g.Select(x => x.ResponsibleForId).ToList()
                };

希望这就是你要找的东西。

答案 3 :(得分:0)

虽然每个人以前的答案对我有帮助(谢谢!),但他们并没有完全奏效。所以我想我会在这里添加我的解决方案,即使用连接,分组和嵌套选择的组合。

from p in db.Persons
where p.Id == IdImSearchingFor
join r in db.Responsibilities on p.Id equals r.ParentId into pr
select new Person
{
    Id = p.Id,
    name = p.Name,
    ResponsibleFor = ( from a in pr
                       where a.ParentId == IdImSearchingFor
                       select new Person
                       {
                           Id = a.Id
                       }).ToList<Person>()
}

答案 4 :(得分:0)

我的建议是使用Lazy加载模式来获取列表,它可能会更好地执行和扩展。用你的例子。

public class Person()
{
  public long Id { get; set; }
  public string Name { get; set;}
  private Lazy<List<long>> _responsibleFor;
  public List<long> ResponsibleFor
  {
    get { return _responsibleFor.Value; }
  }

  public void SetResponsibleFor(Func<long, List<long>> getResponsibleFor)
  {
    _responsibleFor = new Lazy<List<long>>( () => getResponsibleFor(Id) );
  }

  public Person():base() { }
  public Person(long id, string name)
  {
    Id = id;
    Name = name
  }

}

   // Implementation
   var p = new Person(1,"John Doe");
   p.SetResponsibleFor(GetResponsibleForPerson); //Pass a function/method which takes the long for input parameter and outputs List<long>