locked
Regarding a EF linq query RRS feed

  • Question

  • User1183902823 posted

    see the below code

    using (var context = new SchoolDBEntities())
    {
        var stud1 = (from s in context.Students.Include("Standard")
                    where s.StudentName == "Student1"
                    select s).FirstOrDefault<Student>();
    }

    tell me why we need to specify studen inside firstordefault like FirstOrDefault<Student>() ?

    if i write like this way then it will not work ?

    using (var context = new SchoolDBEntities())
    {
        var stud1 = (from s in context.Students.Include("Standard")
                    where s.StudentName == "Student1"
                    select s).FirstOrDefault();
    }

    Tuesday, December 12, 2017 11:08 AM

Answers

  • User991499041 posted

    Hi Tridip1974,

    You can simplify the FirstOrDefault<Student>() to FirstOrDefault(), they're exactly the same.

    Regards,

    zxj

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 2:46 AM
  • User-707554951 posted

    Hi tridip1974, 

    tridip1974

    tell me why we need to specify studen inside firstordefault like FirstOrDefault<Student>() ?

    Specify the type of the elements of source. If we not specify Student in this method, The default type of return is Student ,

    So, You second piece of code will perform as same as first one.

    For this you could see the picture below:

    Related links:

    https://msdn.microsoft.com/en-us/library/bb340482(v=vs.110).aspx

    Best regards 

    Cathy 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 3:23 AM

All replies

  • User991499041 posted

    Hi Tridip1974,

    You can simplify the FirstOrDefault<Student>() to FirstOrDefault(), they're exactly the same.

    Regards,

    zxj

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 2:46 AM
  • User-707554951 posted

    Hi tridip1974, 

    tridip1974

    tell me why we need to specify studen inside firstordefault like FirstOrDefault<Student>() ?

    Specify the type of the elements of source. If we not specify Student in this method, The default type of return is Student ,

    So, You second piece of code will perform as same as first one.

    For this you could see the picture below:

    Related links:

    https://msdn.microsoft.com/en-us/library/bb340482(v=vs.110).aspx

    Best regards 

    Cathy 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 3:23 AM