gpt4 book ai didi

c# - 如何使用 IQueryable 选择全部

转载 作者:太空狗 更新时间:2023-10-29 21:35:37 24 4
gpt4 key购买 nike

使用 MVC3,我有一个 Student Repository(在一个项目中)和一个 StudentService(在另一个项目中)。在服务中,我想创建一个函数来返回 db 表中的所有学生。这对我来说是一种新的做事方式,所以我有点陌生。在下面的 GetAllStudents 函数中,如何更改语法以选择全部。

在存储库中:

      namespace SpeakOut.Data
{
public class StudentRepository
{
SpeakOutDataContext context;
private Table<StudentEntity> table;
public StudentRepository()
{
context = new SpeakOutDataContext();
table = context.GetTable<StudentEntity>();
}


public IQueryable<Student> Select()
{
return table.Select(x => new Student
{
WNumber = x.WNumber,
CatalogueYear = x.CatalogueYear,
Standing = x.Standing
});
}
}

}

在服务中:

   namespace SpeakOut.Services
{
public class StudentService
{
private StudentRepository repository;
public StudentService()
{
repository = new StudentRepository();

}

public IQueryable<Student> GetAllStudents()
{
return repository.Select().All(x => x.FirstName) ; //**This line is where I don't know how I would call all the students**
}


}

最佳答案

您甚至不需要一个空白的 Select,您可以只调用 .AsQueryable,或者只将“table”作为 IQueryable 返回。

关于c# - 如何使用 IQueryable 选择全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9365140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com