LINQ 小项目【组合查询、分页】
时间: 2023-08-07 10:06:50 浏览: 52
好的,我可以为您提供一些关于 LINQ 的小项目,包括组合查询和分页功能。
1. 组合查询
假设您有一张包含学生信息的表格,其中包含学生姓名、学号、性别、年龄、所在班级等字段。您需要实现一个功能,让用户可以根据多个条件进行组合查询。
首先,您需要创建一个包含所有查询条件的数据模型,例如:
```csharp
public class StudentQueryModel
{
public string Name { get; set; }
public string StudentId { get; set; }
public string Gender { get; set; }
public int? Age { get; set; }
public string ClassName { get; set; }
}
```
然后,在查询时使用 LINQ 进行条件筛选,例如:
```csharp
public List<Student> SearchStudents(StudentQueryModel queryModel)
{
using (var db = new MyDbContext())
{
var query = db.Students.AsQueryable();
if (!string.IsNullOrEmpty(queryModel.Name))
{
query = query.Where(s => s.Name.Contains(queryModel.Name));
}
if (!string.IsNullOrEmpty(queryModel.StudentId))
{
query = query.Where(s => s.StudentId.Contains(queryModel.StudentId));
}
if (!string.IsNullOrEmpty(queryModel.Gender))
{
query = query.Where(s => s.Gender == queryModel.Gender);
}
if (queryModel.Age.HasValue)
{
query = query.Where(s => s.Age == queryModel.Age.Value);
}
if (!string.IsNullOrEmpty(queryModel.ClassName))
{
query = query.Where(s => s.ClassName == queryModel.ClassName);
}
return query.ToList();
}
}
```
2. 分页
接下来,假设您需要实现一个分页功能,以便用户可以查看大量数据时能够更加方便地浏览。
首先,在数据模型中添加分页相关的字段:
```csharp
public class StudentQueryModel
{
public string Name { get; set; }
public string StudentId { get; set; }
public string Gender { get; set; }
public int? Age { get; set; }
public string ClassName { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; }
}
```
然后,在查询时使用 Skip 和 Take 方法进行分页,例如:
```csharp
public List<Student> SearchStudents(StudentQueryModel queryModel)
{
using (var db = new MyDbContext())
{
var query = db.Students.AsQueryable();
if (!string.IsNullOrEmpty(queryModel.Name))
{
query = query.Where(s => s.Name.Contains(queryModel.Name));
}
if (!string.IsNullOrEmpty(queryModel.StudentId))
{
query = query.Where(s => s.StudentId.Contains(queryModel.StudentId));
}
if (!string.IsNullOrEmpty(queryModel.Gender))
{
query = query.Where(s => s.Gender == queryModel.Gender);
}
if (queryModel.Age.HasValue)
{
query = query.Where(s => s.Age == queryModel.Age.Value);
}
if (!string.IsNullOrEmpty(queryModel.ClassName))
{
query = query.Where(s => s.ClassName == queryModel.ClassName);
}
var totalCount = query.Count();
var totalPages = (int)Math.Ceiling(totalCount / (double)queryModel.PageSize);
query = query.Skip((queryModel.PageIndex - 1) * queryModel.PageSize).Take(queryModel.PageSize);
return query.ToList();
}
}
```
这样,您就可以实现一个简单的 LINQ 小项目,包括组合查询和分页功能。当然,这只是一个简单的示例,您可以根据实际需求进行扩展和优化。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)