关于EF延时加载的面试题
public async Task<ActionResult> GetData() { var data = (from leftdata in GetLeft() join rightdata in GetRight() on leftdata.Id equals rightdata.Id into tempdata from matchdata in tempdata.DefaultIfEmpty() select matchdata).ToList(); return Ok(data); } private IEnumerable<YourModel> GetLeft() { return YourDbContext.YourModels.Where(c=>c.Id==1); } private IEnumerable<YourModel> GetRight() { return from data in YourDbContext.YourModels select data; }
上面代码调用GetData()查询了几次数据库?
如果换成IQueryable又如何
public async Task<ActionResult> GetData() { var data = (from leftdata in GetLeft() join rightdata in GetRight() on leftdata.Id equals rightdata.Id into tempdata from matchdata in tempdata.DefaultIfEmpty() select matchdata).ToList(); return Ok(data); } private IQueryable<YourModel> GetLeft() { return YourDbContext.YourModels.Where(c=>c.Id==1); } private IQueryable<YourModel> GetRight() { return from data in YourDbContext.YourModels select data; }
使用IQueryable查询了一次数据库,IEnumerable查询了两次
喜欢 C#、SQL、Web