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

C# c如何用sql填充包含列表的复杂对象,c#,sql,list,object,join,C#,Sql,List,Object,Join,我目前有一个使用实体框架的对象模型,它包含一个复杂的对象,其属性是列表。我使用此处建议的第二个选项将其存储在DB中: 但总而言之: Person() { long Id;//primary key string Name; List<long> ResponsibleFor; //list of id } 现在我有点困惑于如何重新填充Person对象。我目前的做法如下: from p in db.Persons join r in db.Responsibi

我目前有一个使用实体框架的对象模型,它包含一个复杂的对象,其属性是列表。我使用此处建议的第二个选项将其存储在DB中:

但总而言之:

Person()
{
  long Id;//primary key
  string Name;
  List<long> ResponsibleFor; //list of id    
}
现在我有点困惑于如何重新填充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 = ?
 }
我一直在尝试重新填充列表属性

有什么建议吗


谢谢

假设您想要获取个人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 
}

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

试一试


看看这个。我已经测试过它将正常的类和列表,而不是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; 
}
以下是两个测试列表

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 } };

希望这就是你想要的。

尽管大家之前的回答帮助我谢谢你!,他们没有完全工作。所以我想我应该在这里添加我的解决方案,即使用连接、分组和嵌套选择的组合

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>()
}

我的建议是使用延迟加载模式来获取列表,它可能会执行得更好,扩展性也更好。用你的例子

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>

除非我用的是EF,p.ResponsibleFor是自动生成的类型,而不是。出于某种原因,此g.Selectx=>x.ResponsibleForId.ToList引发异常:类型为“System.Data.Entity.Infrastructure.ObjectReferenceEqualityComparer”的表达式不能用于类型为“System.Collections.Generic.IEqualityComparer`1[System.Int64]”的构造函数参数这可能有效,但是分组成g似乎失去了人名等其他元素,但事实上不止如此。有没有一种方法可以进行不同的分组,这样我就可以得到p的所有元素,然后从r获得列表?@Norrec您也可以使用dbPersons的整个对象作为分组的键,然后使用它的属性。我编辑了我的答案。我最终在我的解决方案上实现了你的答案,因为它更干净了。谢谢
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()
                };
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>()
}
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>