locked
LINQ inside a Select LINQ - Will it have a performance issue? RRS feed

  • Question

  • User-1950145079 posted

    I want to query a list of person from the database and in the table, it has a column that also contains a list of relatives which is another table in the database. Will this line of code solve the problem? Will it produce a performance issue in the future with thousands of data?

    Refer to the code below:

    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();
    Tuesday, July 3, 2018 1:19 AM

All replies

  • User541108374 posted

    Hi,

    Will it produce a performance issue in the future with thousands of data

    To measure is to know. 

    I've been heavily involved in making applications more performant at my customer and used interceptors in EF to see what actually gets generated for SQL Server. Depending on that I either rewrote the Linq queries or used a view instead. To see the interception part at work go through this article: Viewing the SQL statements Entity Framework produces with interceptors.

    Kris.

    Tuesday, July 3, 2018 5:27 AM
  • User-1950145079 posted

    Hello Kris,

    Thank you for the response. Will check on this article later.

    JP

    Tuesday, July 3, 2018 5:32 AM