首页 > 解决方案 > Select LINQ 中的 LINQ - 会有性能问题吗?

问题描述

我想从数据库中查询人员列表,在表中,它有一列还包含亲属列表,这是数据库中的另一个表。这行代码能解决问题吗?它会在未来产生数千个数据的性能问题吗?

参考下面的代码:

 public class Employee
 {
    public int Id { get; set; }   
    public string Name{ get; set; }
    public virtual List<Relatives> { get; set; }
 }

 public class Relatives
 {
    public int Id { get; set; }   
    public string Name{ get; set; }
    public int EmployeeId { get; set; }
    [ForeignKey("EmployeeId")]
    public virtual Employee Employee { get; set; }
 }

var list = GetDbSet<Employee>().Select(x => new
  {
  id = x.Id,
  name = x.Name,
  ListOfRelatives = string.Join(",", (x.Relatives.Where(y => y.EmployeeId == x.Id).Select(z => z.Name)))
  }.ToList();

如果这不是最好的解决方案,还有其他方法可以解决这个问题吗?

标签: c#entity-frameworklinqlinq-to-entitiesquery-performance

解决方案


推荐阅读