ASP.NET,Linq和查询,如何在我的班级

时间:2016-12-09 10:28:22

标签: c# asp.net linq

请帮助以下

我有模特

public class TitleView
{
    public int Year;        
    public User User;
    public IEnumerable<Students> Student;
    public IEnumerable<Goal> Goals;
}



public class MyController: Controller
{
    private Db db = new Db();
    public ActionResult Index()
    {
        User User = new User();

        Student Student= from a in db.Students where a.Name==User.Name select a;
  此行上的

错误,无法将类型IQueryable转换为   学生。

        var test= (from a in db.Goals where a.StudentID== Student.ID select a).ToList();
  

我可以将第一行转换为List = .... ToList();但是之后   在此行显示错误 - 使用a.StudentID == Student.ID

        var model = new TitleView()
        {
            User = User,
            Student= Student
        };
        return View(model);
    }

我知道这很简单

1 个答案:

答案 0 :(得分:2)

要从列表中获取单个实例,您需要致电FirstFirstOrDefaultSingleSingleOrDefault

在此处详细了解:http://www.dotnettricks.com/learn/linq/understanding-single-singleordefault-first-and-firstordefault