⭐️ Furion v4 版本支持【所有历史版本】无缝升级,一套代码兼容 .NET 5+ ⭐️
Skip to main content

9.10 查询操作

9.10.1 根据主键查询一条

// ==== 同步操作 ====// 示例一var person = repository.Find(1);// 示例二var person = repository.FindOrDefault(1);// 示例三var person = repository.Entities.Find(1);// ==== 异步操作 ====// 示例一var person = await repository.FindAsync(1);// 示例二var person = await repository.FindOrDefaultAsync(1);// 示例三var person = await repository.Entities.FindAsync(1);
小提示

可以支持多个键查询,如主键、联合键。repository.Find(1, "百小僧");

9.10.2 根据条件查询一条

// ==== 同步操作 ====// 示例一var person = repository.Single(u => u.Name == "Furion");// 示例二var person = repository.SingleOrDefault(u => u.Name == "Furion");// 示例三var person = repository.First(u => u.Name == "Furion");// 示例四var person = repository.FirstOrDefault(u => u.Name == "Furion");// 示例五var person = repository.Last(u => u.Name == "Furion");// 示例六var person = repository.LastOrDefault(u => u.Name == "Furion");// 示例七var person = repository.Entities.Single(u => u.Name == "Furion");// 示例八var person = repository.Entities.First(u => u.Name == "Furion");// 示例九var person = repository.Entities.FirstOrDefault(u => u.Name == "Furion");// 示例十var person = repository.Entities.Last(u => u.Name == "Furion");// 示例十一var person = repository.Entities.LastOrDefault(u => u.Name == "Furion");// ==== 异步操作 ====// 示例一var person = await repository.SingleAsync(u => u.Name == "Furion");// 示例二var person = await repository.SingleOrDefaultAsync(u => u.Name == "Furion");// 示例三var person = await repository.FirstAsync(u => u.Name == "Furion");// 示例四var person = await repository.FirstOrDefaultAsync(u => u.Name == "Furion");// 示例五var person = await repository.LastAsync(u => u.Name == "Furion");// 示例六var person = await repository.LastOrDefaultAsync(u => u.Name == "Furion");// 示例七var person = await repository.Entities.SingleAsync(u => u.Name == "Furion");// 示例八var person = await repository.Entities.FirstAsync(u => u.Name == "Furion");// 示例九var person = await repository.Entities.FirstOrDefaultAsync(u => u.Name == "Furion");// 示例十var person = await repository.Entities.LastAsync(u => u.Name == "Furion");// 示例十一var person = await repository.Entities.LastOrDefaultAsync(u => u.Name == "Furion");

9.10.3 查询所有数据

// ==== 同步操作 ====// 示例一var persons = repository.Entities;// 示例二var persons = repository.DetachedEntities;// 示例三var persons = repository.AsQueryable();// 示例四var persons = repository.AsEnumerable();// 示例五var persons = await repository.AsQueryable().ToListAsync();

9.10.4 根据条件查询所有数据

// 示例一var persons = repository.Where(u => u.Id > 10);// 示例二var persons = repository.Where(u => u.Id > 10 && u.Name.Equals("Furion"));// 示例三  (多个 where 里是 "并且",Id>10 and Name == "Furion")var persons = repository.Where(u => u.Id > 10).Where(u => u.Name.Equals("Furion"));// 示例四 (判断 name 是否有值,如果 name 是空的则不会执行 u => u.Id > 10 && u.Name.Equals("Furion"),如果 name 有值就会执行 u => u.Id > 10 && u.Name.Equals("Furion"))var persons = repository.Where(!string.IsNullOrEmpty(name), u => u.Id > 10 && u.Name.Equals("Furion"));// 示例五var persons = repository.Where(!string.IsNullOrEmpty(name), u => u.Id > 10)                        .Where(age > 18, u => u.Name.Contains("百小僧"))                        .Where(u => u.Age > 18);// 示例六 (在一个 where 里用逗号分隔是 "或者",Name=="Furion" or Name == "百小僧" or Name == "MonkSoul")var persons = repository.Where(u => u.Name == "Furion",                                 u => u.Name == "百小僧",                                 u => u.Name == "MonkSoul");// 示例七var persons = repository.Where(u => u.Id > 10).Where(u => u.Name.Equals("Furion"))                        .Where(age > 18, u => u.Name.Contains("百小僧"))                        .Where(u => u.Name == "Furion",                                 u => u.Name == "百小僧",                                 u => u.Name == "MonkSoul");// 示例八var persons = repository.Where((age > 18, u => u.Name == "Furion"),                                   (!string.IsNullOrEmpty(name), u => u.Id > 10));// 示例九var persons = repository.Where(u => u.Id > 10).Where(u => u.Name.Equals("Furion"))                        .Where(age > 18, u => u.Name.Contains("百小僧"))                        .Where(u => u.Name == "Furion",                                 u => u.Name == "百小僧",                                 u => u.Name == "MonkSoul")                        .Where((age > 18, u => u.Name == "Furion"),                                   (!string.IsNullOrEmpty(name), u => u.Id > 10));// 示例十var persons = repository.Entities.Where(u => u.Id > 10)                                 .Where(age > 18, u => u.Name.Contains("百小僧"));// 示例十一var persons = repository.DetachedEntities.Where(u => u.Id > 20);// 示例十二var persons = repository.AsQueryable(u => u.Id > 20).Where(u => u.Name == "Furion");

9.10.5 分页查询

// ==== 同步操作 ====// 示例一var persons = repository.Where(u => u.Id > 10).ToPagedList();// 示例二var persons = repository.Where(u => u.Id > 10).ToPagedList(1, 10);// ==== 异步操作 ====// 示例一var persons = await repository.Where(u => u.Id > 10).ToPagedListAsync();// 示例二var persons = await repository.Where(u => u.Id > 10).ToPagedListAsync(1, 10);

9.10.6 其他查询

9.10.6.1 查看记录是否存在

// ==== 同步操作 ====// 示例一var isExists = repository.Any();// 示例二var isExists = repository.Any(u => u.Id > 10);// 示例三var isExists = repository.Entities.Any();// 示例四var isExists = repository.DetachedEntities.Any(u => u.Id > 10);// 示例五var isExists = repository.Where(u => u.Id > 10).Any();// ==== 异步操作 ====// 示例一var isExists = await repository.AnyAsync();// 示例二var isExists = await repository.AnyAsync(u => u.Id > 10);// 示例三var isExists = await repository.Entities.AnyAsync();// 示例四var isExists = await repository.DetachedEntities.AnyAsync(u => u.Id > 10);// 示例五var isExists = await repository.Where(u => u.Id > 10).AnyAsync();

9.10.6.2 查询记录数

// ==== 同步操作 ====// 示例一var count = repository.Count();// 示例二var count = repository.Count(u => u.Id > 10);// 示例三var count = repository.Entities.Count(u => u.Id > 10);// 示例四var count = repository.Entities.DetachedEntities.Count();// 示例五var count = repository.Where(u => u.Id > 10).Count();// ==== 异步操作 ====// 示例一var count = await repository.CountAsync();// 示例二var count = await repository.CountAsync(u => u.Id > 10);// 示例三var count = await repository.Entities.CountAsync(u => u.Id > 10);// 示例四var count = await repository.Entities.DetachedEntities.CountAsync();// 示例五var count = await repository.Where(u => u.Id > 10).CountAsync();

9.10.6.3 查询最大值

// ==== 同步操作 ====// 示例一var entity = repository.Max();// 示例二var value = repository.Max(u => u.Id);// 示例三var entity = repository.Entities.Max();// 示例四var value = repository.Entities.DetachedEntities.Max(u => u.Age);// 示例五var value = repository.Where(u => u.Id > 10).Max(u => u.Age);// ==== 异步操作 ====// 示例一var entity = await repository.MaxAsync();// 示例二var value = await repository.MaxAsync(u => u.Id);// 示例三var entity = await repository.Entities.MaxAsync();// 示例四var value = await repository.Entities.DetachedEntities.MaxAsync(u => u.Age);// 示例五var value = await repository.Where(u => u.Id > 10).MaxAsync(u => u.Age);

9.10.6.4 查询最小值

// ==== 同步操作 ====// 示例一var entity = repository.Min();// 示例二var value = repository.Min(u => u.Id);// 示例三var entity = repository.Entities.Min();// 示例四var value = repository.Entities.DetachedEntities.Min(u => u.Age);// 示例五var value = repository.Where(u => u.Id > 10).Min(u => u.Age);// ==== 异步操作 ====// 示例一var entity = await repository.MinAsync();// 示例二var value = await repository.MinAsync(u => u.Id);// 示例三var entity = await repository.Entities.MinAsync();// 示例四var value = await repository.Entities.DetachedEntities.MinAsync(u => u.Age);// 示例五var value = await repository.Where(u => u.Id > 10).MinAsync(u => u.Age);

9.10.6.5 求和查询

// ==== 同步操作 ====// 示例一var sum = repository.Entities.Sum(u => u.Cost);// 示例二var sum = repository.AsQueryable().Sum(u => u.Cost);// 示例三var sum = repository.DetachedEntities.Sum(u => u.Cost);// ==== 异步操作 ====// 示例一var sum = await repository.Entities.SumAsync(u => u.Cost);// 示例二var sum = await repository.AsQueryable().SumAsync(u => u.Cost);// 示例三var sum = await repository.DetachedEntities.SumAsync(u => u.Cost);

9.10.6.6 求平均值查询

// ==== 同步操作 ====// 示例一var sum = repository.Entities.Average(u => u.Cost);// 示例二var sum = repository.AsQueryable().Average(u => u.Cost);// 示例三var sum = repository.DetachedEntities.Average(u => u.Cost);// ==== 异步操作 ====// 示例一var sum = await repository.Entities.AverageAsync(u => u.Cost);// 示例二var sum = await repository.AsQueryable().AverageAsync(u => u.Cost);// 示例三var sum = await repository.DetachedEntities.AverageAsync(u => u.Cost);

9.10.6.7 时间查询

var starDate = DateTime.Parse("2020-09-10");var endDate = DateTime.Parse("2020-09-10");var query = repository.Where(u => u.CreatedDt >= starDate && u.CreatedDt <= endDate);

9.10.6.8 模糊查询

// 示例一repository.Where(u => u.Name.StartsWith("Furion"));// 示例二_testRepository.Where(u => u.Name.EndsWith("Furion"));// 示例三_testRepository.Where(u => u.Name.Contains("Furion"));

9.10.6.9 Case When

数据库中的 Case When 实际上对应的是我们程序中的 三元表达式 ,也就是使用 三元表达式 即可自动生成 Case When 语句。

9.10.7 反馈与建议

与我们交流

给 Furion 提 Issue

演练场