Lazy loading or linq query?

As stated in this thread, I wonder what happens faster and better.

Linq and a new method in a repository such as GetCourts (int clubId)

var courts =_context.Courts.Where(c=>c.ClubId==clubId)

Or lazy loading using EF

var courts= _clubRepository.GetClub(clubId).Courts;

My club entity:

public class Club :EntityBase
    {
        public string Name { get; set; }
        public virtual IList<Court> Courts { get; set; }
    }

Judge of my court:

public class Court:EntityBase
    {
       public bool IsIndoor { get; set; }

       public int ClubId { get; set; }
       public virtual Club Club { get; set; }

       public int CourtTypeId { get; set; }
       public virtual CourtType CourtType { get; set; }
    }

I do not know which approach to use in my project.

+4
source share
2 answers

In the example you are using, which you describe in your question, using LINQ will be much faster than using lazy loading.

This statement:

var courts= _clubRepository.GetClub(clubId).Courts;

equivalent to these operators:

var club = _clubRepository.GetClub(clubId);
var courts = club.Courts;

SQL ClubId == clubId. SQL, ClubId == clubId. , , - , sp_executesql.

,

var courts =_context.Courts.Where(c=>c.ClubId==clubId)

SQL-, .

, Club , , , , .

+4

Lazy loading sp_executesql, linq t-sql .

msdn , sp_executesql , , , .

sp_executesql - SQL . sp_executesql Transact-SQL , , Transact-SQL. Transact-SQL , SQL Server, , . . - MSDN

Lazy loading - , , , , .

+3

All Articles