locked
How do I write the predicate to test for more than one value? RRS feed

  • Question

  • User1929428035 posted

    I have: 

    return this.ObjectContext.StudentGrades.Include("Student").Include("Subject").Where(s => s.StudentID == intStudentID()).OrderBy(s => s.ID);

    Which returns one student. However a parent has access to the StudentGrades and may have one or more students enrolled. I can write an SQL query to get the data I want but I do not know how to apply it to the Where clause. 

    Please help.

    Monday, April 29, 2013 7:31 AM

Answers

  • User281315223 posted

    The code that you provided will return the StudentID for every student that has the ParentID that you specify. You can use the code that I previously provided if you want to use multiple ParentIDs. 

    //This is an example of your existing query
    return ObjectContext.StudentGrades.Include("Student").Include("Subject").Where(s => s.ParentID == intParentID);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 30, 2013 8:18 AM

All replies

  • User281315223 posted

    The code that you have should return any number of elements within your sequence that have the StudentID specified (it may just so happen that there is only one that meets that constraint).

    If you want to test for multiple values, you can use the Or operator || to seperate your specific IDs : 

    //IDs to check for
    int example1 = 1;
    int example2 = 2;
    
    //Perform your query
    return this.ObjectContext.StudentGrades.Include("Student").Include("Subject").Where(s => s.StudentID == example1 || example2).OrderBy(s => s.ID);

    If you want to use a different approach, you could store the values that you are going to check for within a collection such as a List or an Array and use the Any() method to check if the value is contained within the collection : 

    //IDs to check
    List<int> idsToCheck = new List<int>(){ 1 , 2 };
    
    //Your query
    return this.ObjectContext.StudentGrades.Include("Student").Include("Subject").Where(s => idsToCheck.Any(i => i == s.StudentID)).OrderBy(s => s.ID);




    Monday, April 29, 2013 8:12 PM
  • User1929428035 posted

    Thank you for your response. What I want to do is test against something like the following SQL Query:

    SELECT StudentID FROM ParentStudent WHERE ParentID = intParentID.

    The query will return as much rows as the Parent has students.

    Monday, April 29, 2013 9:53 PM
  • User281315223 posted

    The code that you provided will return the StudentID for every student that has the ParentID that you specify. You can use the code that I previously provided if you want to use multiple ParentIDs. 

    //This is an example of your existing query
    return ObjectContext.StudentGrades.Include("Student").Include("Subject").Where(s => s.ParentID == intParentID);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 30, 2013 8:18 AM
  • User1929428035 posted

    Thank you so very much Rion. I tried as you suggested and it works better than a charm.

    Tuesday, April 30, 2013 11:50 AM