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; }
由于IEnumerable和IQueryable的特性,我原本的想法是使用IEnumerable一共查询了3次数据库,使用IQueryable查询了一次数据库,最后实践发现无论是那种方式都只查询了一次数据库
标签:面试题,return,IQueryable,EF,IEnumerable,private,延时,data,Id From: https://www.cnblogs.com/daiwk/p/18110339