简体   繁体   English

在数据层ASP.net MVC中过滤

[英]Filtering in data layer ASP.net MVC

Overview: I need to display different tables per year level from the students database table. 概述:我需要从学生数据库表中每年显示不同的表级别。 When the administrator click the button for 1st year it will display all 1st year students (other filtering and sorting are in table). 当管理员单击第一年级的按钮时,它将显示所有第一年级的学生(其他过滤和排序在表中)。 Also, it will also act as service to other apps that's why i cannot just use javascript/html on this (i've tried too slow). 另外,它还可以作为其他应用程序的服务,这就是为什么我不能在此上仅使用javascript / html(我尝试过太慢)的原因。 This is a report generation/query app for a sample school project. 这是一个示例学校项目的报告生成/查询应用程序。

Other apps must access it in a url like: http://localhost:6342/Students/GetYearLvl(1) 其他应用程序必须通过以下网址访问它: http://localhost:6342/Students/GetYearLvl(1)

There are four html display tables: 1st year, 2nd year, 3rd year, and 4th year and each students profile when clicked on the table 共有四个html显示表:第一年,第二年,第三年和第四年,每个学生的个人资料在单击表时均会显示

I was able to do the YearLvl but when i tried to load a specific user it doesn't show 我能够执行YearLvl,但是当我尝试加载特定用户时却没有显示

//Get ALL STUDENTS -- Required
public IQueryable<RequestStudents> Get()
{
    StudentsModelContainer context = DbContextFactory<StudentsModelContainer>.Create();
        var result = context.RequestStudentSet.Where(x => x.YearLvl == 1);
    return result;
}

//
public IQueryable<RequestStudents> Get(int yearLevel)
{
    StudentsModelContainer context = DbContextFactory<StudentsModelContainer>.Create();
    if (yearLevel == 1 or yearLevel == 2 or yearLevel == 3 or yearLevel == 4){
        var result = context.RequestStudentSet.Where(x => x.YearLvl == yearLevel);
        return result;
    }
    else
    {
         var result = context.RequestStudentSet.Where(x => x.YearLvl == yearLevel);
          return result;
    }
}

When i try first or default it gives error like cannot convert RequestStudentSet. 当我第一次尝试或默认时,它会给出错误,例如无法转换RequestStudentSet。

Please do help. 请帮忙。 Not quite familiar in ASp.net C# MVC 对ASp.net C#MVC不太熟悉

You should change your return from 您应该更改您的退货

public IQueryable<RequestStudents> Get(int yearLevel)

to

public RequestStudents Get(int yearLevel) 

and change to 并更改为

var result = context.RequestStudentSet.Where(x => x.YearLvl == yearLevel).FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM