none
Add database field to a Entity framework Model Fluent API LINQ context expression to act as a join in data retrieval to inform a datagrid populated with bound fields. RRS feed

  • Question

  • I am working with code designed by an advanced programmer that was handed down to me.  The technologies in use are Visual Sudio 2010, SQL Server, C#.NET objects, ADO.NET Entity Data Model, LINQ Fluent API, DataGrid, and Boundfields (yeah... I'm screwed).  There is a Fluent API LINQ query that is being used to retrieve student data.  There is a list of fields in the query that map to boundfields in a datagrid.  I can handle the boundfields in the datagrid easily enough.  The LINQ query uses something called a "context" ("StudentSet") that seems to be associated with the "Student" class.  There is another table in the database that has a "Semester" field.  I want to limit the query to show only students in a particular semester.  When I try to create a LINQ Fluent API "Where" clause in the LINQ query (VS2010), the dot notation does not "see" the "Semester" field in the other table.  My guess is that the field is not part of the "Student" context.  When I replace the "Student" with the "Registration" context/table the dot notation can see the "Semester" field.  If it were SQL I could simply make a join to limit the query by semester but things are not so simple.  Where to start to resolve this issue?  Suggestions please.

    Need to do something like:

    .Where(x => x.StudentId = x.Registrations.StudentID);

    Hover error over StudentID:

    'System.Data.Objects.DataClasses.EntityCollection<WebClient.Domain.Registration>' does not contain a definition for 'StudentID' and no extension method 'StudentID' accepting a first argument of type 'System.Data.Objects.DataClasses.EntityCollection<WebClient.Domain.Registration>' could be found (are you missing a using directive or an assembly reference?)

    What?

    (No I don't know how to do joins in LINQ Fluent API)

    Example LINQ:

    var studentlist =

      context

        .StudentSet

        .Where(x => x.IsDeleted == false)

        .OrderBy(x => x.StudentInformation.LastName)

        .Select(r => new RegisteredStudentRecord

        {

          StudentId = r.StudentId,

          FirstName = r.StudentInformation.FirstName,

          LastName = r.StudentInformation.LastName,

          SchoolName = r.AcademicInfo.SchoolName,

          GradeLevel = r.CurrentGrade,

          PrimaryCode = r.Registrations.Where(p => p.Rank == 1).FirstOrDefault().Course.Code,

          PrimaryName = r.Registrations.Where(p => p.Rank == 1).FirstOrDefault().Course.Name,

                    });





    • Edited by jeffa2008 Tuesday, July 29, 2014 2:00 PM
    Tuesday, July 29, 2014 1:35 PM

Answers

  • Solved!

    The line:

         public int SemesterID { get; set; }

    was added to the beginning of the page and the following additional ".Where" clause was added to the query:

         .Where(x => x.Registrations.FirstOrDefault().SemesterId == SemesterId)

    That solved the issue.  Thanks to my coworker Mike for figuring this out!

    • Edited by jeffa2008 Tuesday, July 29, 2014 6:38 PM Solved
    • Marked as answer by Fred BaoModerator Thursday, August 7, 2014 6:39 AM
    Tuesday, July 29, 2014 6:33 PM