How do I get a response based on two different IDs in my API?










0















public class Report

[Key]
public int ReportId get; set;

[ForeignKey("Subjects")]
public int SubjectId get; set;
public Subjects Subjects get; set;

[ForeignKey("Teacher")]
public int TeacherId get; set;
public Teacher Teacher get; set;

[ForeignKey("MarkType")]
public int MarkTypeId get; set;
public MarkType MarkType get; set;


public class Teacher

[Key]
public int TeacherId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;



public class Student

[Key]
public int StudentId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;

[ForeignKey("Grade")]
public int GradeId get; set;
public Grade Grade get; set;


public class Grade

[Key]
public int GradeId get; set;
public int StudentGrade get; set;


public class Subjects

[Key]
public int SubjectId get; set;

[MaxLength(50)]
[Required]
public string SubjectName get; set;


public class Terms

[Key]
public int TermId get; set;

public int Term get; set;


public class MarkType

[Key]
public int MarkTypeId get; set;
[MaxLength(20)]
[Required]
public string TypeName get; set;



public class StudentMark

[Key]
public int StudentMarkId get; set;

[ForeignKey("Report")]
public int ReportId get; set;
public Report Report get; set;

[ForeignKey("Student")]
public int StudentId get; set;
public Student Student get; set;

public int Mark get; set;

[ForeignKey("Terms")]
public int TermId get; set;
public Terms Terms get; set;




In the API I am making I want to have the ability to use two different IDs to get a more specific response.



var report = ReportDBContext.StudentMark
.Include(p => p.Student.Grade).Include(p => p.Report)
.Include(p => p.Terms).Include(a => a.Report.Subjects).Include(a => a.Terms)
.Include(a => a.Report.MarkType).Include(a => a.Report.Teacher).ToList();


This allowed me to get StudentMark as well as it's related entities but I want to have the ability to use The student's Id and the Term's Id to get a student's marks for that term and all the subjects related to the student. I am a beginner to Web API so please let me know if I need to add more context.










share|improve this question
























  • Sure, no problem

    – user10174623
    Nov 15 '18 at 11:01















0















public class Report

[Key]
public int ReportId get; set;

[ForeignKey("Subjects")]
public int SubjectId get; set;
public Subjects Subjects get; set;

[ForeignKey("Teacher")]
public int TeacherId get; set;
public Teacher Teacher get; set;

[ForeignKey("MarkType")]
public int MarkTypeId get; set;
public MarkType MarkType get; set;


public class Teacher

[Key]
public int TeacherId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;



public class Student

[Key]
public int StudentId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;

[ForeignKey("Grade")]
public int GradeId get; set;
public Grade Grade get; set;


public class Grade

[Key]
public int GradeId get; set;
public int StudentGrade get; set;


public class Subjects

[Key]
public int SubjectId get; set;

[MaxLength(50)]
[Required]
public string SubjectName get; set;


public class Terms

[Key]
public int TermId get; set;

public int Term get; set;


public class MarkType

[Key]
public int MarkTypeId get; set;
[MaxLength(20)]
[Required]
public string TypeName get; set;



public class StudentMark

[Key]
public int StudentMarkId get; set;

[ForeignKey("Report")]
public int ReportId get; set;
public Report Report get; set;

[ForeignKey("Student")]
public int StudentId get; set;
public Student Student get; set;

public int Mark get; set;

[ForeignKey("Terms")]
public int TermId get; set;
public Terms Terms get; set;




In the API I am making I want to have the ability to use two different IDs to get a more specific response.



var report = ReportDBContext.StudentMark
.Include(p => p.Student.Grade).Include(p => p.Report)
.Include(p => p.Terms).Include(a => a.Report.Subjects).Include(a => a.Terms)
.Include(a => a.Report.MarkType).Include(a => a.Report.Teacher).ToList();


This allowed me to get StudentMark as well as it's related entities but I want to have the ability to use The student's Id and the Term's Id to get a student's marks for that term and all the subjects related to the student. I am a beginner to Web API so please let me know if I need to add more context.










share|improve this question
























  • Sure, no problem

    – user10174623
    Nov 15 '18 at 11:01













0












0








0








public class Report

[Key]
public int ReportId get; set;

[ForeignKey("Subjects")]
public int SubjectId get; set;
public Subjects Subjects get; set;

[ForeignKey("Teacher")]
public int TeacherId get; set;
public Teacher Teacher get; set;

[ForeignKey("MarkType")]
public int MarkTypeId get; set;
public MarkType MarkType get; set;


public class Teacher

[Key]
public int TeacherId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;



public class Student

[Key]
public int StudentId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;

[ForeignKey("Grade")]
public int GradeId get; set;
public Grade Grade get; set;


public class Grade

[Key]
public int GradeId get; set;
public int StudentGrade get; set;


public class Subjects

[Key]
public int SubjectId get; set;

[MaxLength(50)]
[Required]
public string SubjectName get; set;


public class Terms

[Key]
public int TermId get; set;

public int Term get; set;


public class MarkType

[Key]
public int MarkTypeId get; set;
[MaxLength(20)]
[Required]
public string TypeName get; set;



public class StudentMark

[Key]
public int StudentMarkId get; set;

[ForeignKey("Report")]
public int ReportId get; set;
public Report Report get; set;

[ForeignKey("Student")]
public int StudentId get; set;
public Student Student get; set;

public int Mark get; set;

[ForeignKey("Terms")]
public int TermId get; set;
public Terms Terms get; set;




In the API I am making I want to have the ability to use two different IDs to get a more specific response.



var report = ReportDBContext.StudentMark
.Include(p => p.Student.Grade).Include(p => p.Report)
.Include(p => p.Terms).Include(a => a.Report.Subjects).Include(a => a.Terms)
.Include(a => a.Report.MarkType).Include(a => a.Report.Teacher).ToList();


This allowed me to get StudentMark as well as it's related entities but I want to have the ability to use The student's Id and the Term's Id to get a student's marks for that term and all the subjects related to the student. I am a beginner to Web API so please let me know if I need to add more context.










share|improve this question
















public class Report

[Key]
public int ReportId get; set;

[ForeignKey("Subjects")]
public int SubjectId get; set;
public Subjects Subjects get; set;

[ForeignKey("Teacher")]
public int TeacherId get; set;
public Teacher Teacher get; set;

[ForeignKey("MarkType")]
public int MarkTypeId get; set;
public MarkType MarkType get; set;


public class Teacher

[Key]
public int TeacherId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;



public class Student

[Key]
public int StudentId get; set;

[MaxLength(50)]
[Required]
public string FName get; set;

[MaxLength(50)]
[Required]
public string LName get; set;

[ForeignKey("Grade")]
public int GradeId get; set;
public Grade Grade get; set;


public class Grade

[Key]
public int GradeId get; set;
public int StudentGrade get; set;


public class Subjects

[Key]
public int SubjectId get; set;

[MaxLength(50)]
[Required]
public string SubjectName get; set;


public class Terms

[Key]
public int TermId get; set;

public int Term get; set;


public class MarkType

[Key]
public int MarkTypeId get; set;
[MaxLength(20)]
[Required]
public string TypeName get; set;



public class StudentMark

[Key]
public int StudentMarkId get; set;

[ForeignKey("Report")]
public int ReportId get; set;
public Report Report get; set;

[ForeignKey("Student")]
public int StudentId get; set;
public Student Student get; set;

public int Mark get; set;

[ForeignKey("Terms")]
public int TermId get; set;
public Terms Terms get; set;




In the API I am making I want to have the ability to use two different IDs to get a more specific response.



var report = ReportDBContext.StudentMark
.Include(p => p.Student.Grade).Include(p => p.Report)
.Include(p => p.Terms).Include(a => a.Report.Subjects).Include(a => a.Terms)
.Include(a => a.Report.MarkType).Include(a => a.Report.Teacher).ToList();


This allowed me to get StudentMark as well as it's related entities but I want to have the ability to use The student's Id and the Term's Id to get a student's marks for that term and all the subjects related to the student. I am a beginner to Web API so please let me know if I need to add more context.







entity-framework api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:01

























asked Nov 15 '18 at 8:29







user10174623



















  • Sure, no problem

    – user10174623
    Nov 15 '18 at 11:01

















  • Sure, no problem

    – user10174623
    Nov 15 '18 at 11:01
















Sure, no problem

– user10174623
Nov 15 '18 at 11:01





Sure, no problem

– user10174623
Nov 15 '18 at 11:01












1 Answer
1






active

oldest

votes


















0














If you want to query by either StudentId or TermId, I suggest that you provide two different endpoints for these two different queries. Use LINQ Where to check your conditions.



public StudentMark GetMarksByStudentId(int studentId) 
return ReportDBContext.StudentMark
/* .Include(...) */
.Where(mark => mark.StudentId == studentId)
.ToArray();


public StudentMark GetMarksByTermId(int termId)
return ReportDBContext.StudentMark
/* .Include(...) */
.Where(mark => mark.TermId == termId)
.ToArray();



If you want to query by StudentId and TermId simultaneously, introduce a query object to encapsulate your parameters. You can test for multiple conditions in the Where clause with AND &&.



public StudentMark FindMarks(StudentMarkQuery query) 
return ReportDBContext.StudentMark
/* .Include(...) */
.Where(mark => mark.StudentId == query.StudentId
&& mark.TermId == query.TermId)
.ToArray();



The StudentMarkQuery class is introduced so you can add additional parameters without changing the overall signature of the endpoint:



public class StudentMarkQuery 
public int StudentId get; set;
public int TermId get; set;






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315189%2fhow-do-i-get-a-response-based-on-two-different-ids-in-my-api%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you want to query by either StudentId or TermId, I suggest that you provide two different endpoints for these two different queries. Use LINQ Where to check your conditions.



    public StudentMark GetMarksByStudentId(int studentId) 
    return ReportDBContext.StudentMark
    /* .Include(...) */
    .Where(mark => mark.StudentId == studentId)
    .ToArray();


    public StudentMark GetMarksByTermId(int termId)
    return ReportDBContext.StudentMark
    /* .Include(...) */
    .Where(mark => mark.TermId == termId)
    .ToArray();



    If you want to query by StudentId and TermId simultaneously, introduce a query object to encapsulate your parameters. You can test for multiple conditions in the Where clause with AND &&.



    public StudentMark FindMarks(StudentMarkQuery query) 
    return ReportDBContext.StudentMark
    /* .Include(...) */
    .Where(mark => mark.StudentId == query.StudentId
    && mark.TermId == query.TermId)
    .ToArray();



    The StudentMarkQuery class is introduced so you can add additional parameters without changing the overall signature of the endpoint:



    public class StudentMarkQuery 
    public int StudentId get; set;
    public int TermId get; set;






    share|improve this answer



























      0














      If you want to query by either StudentId or TermId, I suggest that you provide two different endpoints for these two different queries. Use LINQ Where to check your conditions.



      public StudentMark GetMarksByStudentId(int studentId) 
      return ReportDBContext.StudentMark
      /* .Include(...) */
      .Where(mark => mark.StudentId == studentId)
      .ToArray();


      public StudentMark GetMarksByTermId(int termId)
      return ReportDBContext.StudentMark
      /* .Include(...) */
      .Where(mark => mark.TermId == termId)
      .ToArray();



      If you want to query by StudentId and TermId simultaneously, introduce a query object to encapsulate your parameters. You can test for multiple conditions in the Where clause with AND &&.



      public StudentMark FindMarks(StudentMarkQuery query) 
      return ReportDBContext.StudentMark
      /* .Include(...) */
      .Where(mark => mark.StudentId == query.StudentId
      && mark.TermId == query.TermId)
      .ToArray();



      The StudentMarkQuery class is introduced so you can add additional parameters without changing the overall signature of the endpoint:



      public class StudentMarkQuery 
      public int StudentId get; set;
      public int TermId get; set;






      share|improve this answer

























        0












        0








        0







        If you want to query by either StudentId or TermId, I suggest that you provide two different endpoints for these two different queries. Use LINQ Where to check your conditions.



        public StudentMark GetMarksByStudentId(int studentId) 
        return ReportDBContext.StudentMark
        /* .Include(...) */
        .Where(mark => mark.StudentId == studentId)
        .ToArray();


        public StudentMark GetMarksByTermId(int termId)
        return ReportDBContext.StudentMark
        /* .Include(...) */
        .Where(mark => mark.TermId == termId)
        .ToArray();



        If you want to query by StudentId and TermId simultaneously, introduce a query object to encapsulate your parameters. You can test for multiple conditions in the Where clause with AND &&.



        public StudentMark FindMarks(StudentMarkQuery query) 
        return ReportDBContext.StudentMark
        /* .Include(...) */
        .Where(mark => mark.StudentId == query.StudentId
        && mark.TermId == query.TermId)
        .ToArray();



        The StudentMarkQuery class is introduced so you can add additional parameters without changing the overall signature of the endpoint:



        public class StudentMarkQuery 
        public int StudentId get; set;
        public int TermId get; set;






        share|improve this answer













        If you want to query by either StudentId or TermId, I suggest that you provide two different endpoints for these two different queries. Use LINQ Where to check your conditions.



        public StudentMark GetMarksByStudentId(int studentId) 
        return ReportDBContext.StudentMark
        /* .Include(...) */
        .Where(mark => mark.StudentId == studentId)
        .ToArray();


        public StudentMark GetMarksByTermId(int termId)
        return ReportDBContext.StudentMark
        /* .Include(...) */
        .Where(mark => mark.TermId == termId)
        .ToArray();



        If you want to query by StudentId and TermId simultaneously, introduce a query object to encapsulate your parameters. You can test for multiple conditions in the Where clause with AND &&.



        public StudentMark FindMarks(StudentMarkQuery query) 
        return ReportDBContext.StudentMark
        /* .Include(...) */
        .Where(mark => mark.StudentId == query.StudentId
        && mark.TermId == query.TermId)
        .ToArray();



        The StudentMarkQuery class is introduced so you can add additional parameters without changing the overall signature of the endpoint:



        public class StudentMarkQuery 
        public int StudentId get; set;
        public int TermId get; set;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 11:19









        Georg PatscheiderGeorg Patscheider

        7,54211428




        7,54211428





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315189%2fhow-do-i-get-a-response-based-on-two-different-ids-in-my-api%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線