-1

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). 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)

There are four html display tables: 1st year, 2nd year, 3rd year, and 4th year and each students profile when clicked on the table

I was able to do the YearLvl but when i tried to load a specific user it doesn't show

//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.

Please do help. Not quite familiar in ASp.net C# MVC

8
  • 2
    No where in your code have you shown anything relating to filtering by a user, or .FirstOrDefault() (and what is the point of your if/else block in the 2nd example - both do exactly the same thing) Commented Oct 24, 2017 at 11:00
  • That code is working and is showing the record of a specific student (service). but when i try to get it into the UI it doesn't show. Commented Oct 24, 2017 at 11:11
  • 1
    You have not shown any code relating to your view either. Commented Oct 24, 2017 at 11:11
  • I guess, your context does not exist when other apps calls the method, try converting the result to .ToList< RequestStudentSet>() Commented Oct 24, 2017 at 11:20
  • 1
    IQueryable<T> is a collection, you want a single value, so you cannot use it to return a single value Commented Oct 24, 2017 at 11:27

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

7 Comments

How is FirstOrDefault going to give all students in a "year"? Also, returning an enumerable is better api etiquette.
yes that's my problem how can i both do that using one key i have reserved the first 20 (0-19) id (primary key) for this
he asked about FirstorDefault, I answered how he can accomplish that, not an etiquette question
Maybe I have misunderstood, edit your answer so I can un-downvote.
that's ok @Crowcoder, the question is a little confusing. BoogerT, keep the method returning list and call FirstOrDefault eg students.Get(1).FirstOrDefault()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.