Jerome Agda
Jerome Agda

Reputation: 11

Joining 2 Tables using Linq to return as list

Here's my WebGrid Code:

@grid.Table(
            tableStyle: "table table-responsive table-bordered",
            columns: grid.Columns(
                grid.Column(format:@<text> <input type="checkbox" name="check[]" code="@item.SubjectName"  id="[email protected]" value="[email protected]"  /></text>, header: "Check"),
                grid.Column(columnName: "SubjectCode", header: "SubjectCode"),
                grid.Column(columnName: "SubjectName", header: "SubjectName"),
                grid.Column(columnName: "DescriptiveTitle", header: "DescriptiveTitle"),
                grid.Column(columnName: "TotalUnits", header: "Units"),
                grid.Column(columnName: "Schedule", header: "Schedule"),
                grid.Column(columnName: "Instructor", header: "Instructor"),
                grid.Column(columnName: "Room", header: "Room")
            )

        )

Here's what's on my controller:

using (dc)
        {
            var v = (from a in dc.Subjects
                     from b
                     in dc.Curricula
                          .Where(o => a.SubjectCode == o.CourseCode)
                          .DefaultIfEmpty()
                     where
                            a.SubjectCode.Contains(search) ||
                            a.SubjectName.Contains(search) ||
                            a.Curriculum.DescriptiveTitle.Contains(search) ||
                            a.Schedule.Contains(search) ||
                            a.Instructor.Contains(search) ||
                            a.Room.Contains(search)
                     select new { Subject = a, Curriculum = b}
                     );

            totalRecord = v.Count();
            v = v.OrderBy(x => x.Curriculum.Year).ThenBy(x => x.Curriculum.Sem);
            return v.ToList();
        }

I'm having problem on foreign data ("DescriptiveTitle","TotalUnits") to be called in webgrid. I've tried other suggested codes that I've found but still not working.

I tried joining the 2 table using linq but the line:

return v.ToList();

Is in error. I appreciate if someone can help me.

CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: UESLProject.Subject Subject, UESLProject.Curriculum Curriculum>>' to 'System.Collections.Generic.List<UESLProject.Subject>' UESLProject C:\Users\jerome agda\documents\visual studio 2015\Projects\UESLProject\UESLProject\Controllers\AccountController.cs 162 Active

Upvotes: 0

Views: 70

Answers (1)

meJustAndrew
meJustAndrew

Reputation: 6613

The problem is not the return statement itself, but the actual type which your method excepts to be returned which is UESLProject.Subject and you return a list of anonymous types. If you need to return a list of objects which contain both a Subject and a Curriculum, then you will need to change the signature of your method and make a separate class wrapping these two (unless you want to use a key value paired collection or Tuple)

Here is a short example:

class Wrapper
{
    public Subject Subject { get; set; }
    public Curriculum Curriculum { get; set; }
}

List<Wrapper> YourMethodName(...)
{
    ...
        {
            var v = (from a in dc.Subjects
                     from b
                     in dc.Curricula
                          .Where(o => a.SubjectCode == o.CourseCode)
                          .DefaultIfEmpty()
                     where
                            a.SubjectCode.Contains(search) ||
                            a.SubjectName.Contains(search) ||
                            a.Curriculum.DescriptiveTitle.Contains(search) ||
                            a.Schedule.Contains(search) ||
                            a.Instructor.Contains(search) ||
                            a.Room.Contains(search)
                     select new Wrapper { Subject = a, Curriculum = b});

            totalRecord = v.Count();
            v = v.OrderBy(x => x.Curriculum.Year).ThenBy(x => x.Curriculum.Sem);
            return v.ToList();
}

Upvotes: 1

Related Questions