left join
var query = from log in logRepository.Table.Include(a => a.User) join user in logRepository.Table on log.UserID equals user.ID into lu from lu1 in lu.DefaultIfEmpty() select new { log.Title,log.Description,log.User.Name};
生成的sql
SELECT [log].[ID], [log].[Name], [log].[CompanyName], [log.User].[Password] FROM [Log] AS [log] LEFT JOIN [User] AS [log.User] ON [log].[UserID] = [log.User].[ID] LEFT JOIN [User] AS [user] ON [log].[UserID] = [user].[ID]
log.UserID 为可空类型,否则生成的sql中会是inner jion
延迟加载
建议不要使用
每次用到延迟加载属性,都会查一次数据库;
比如列表中(100行)访问延迟加载属性,会多查100次数据库;
比如序列化的时候,也会根据延迟加载属性查询数据库;