C#: Fill DataGridView From Anonymous Linq Query

Posted by mdvaldosta on Stack Overflow See other posts from Stack Overflow or by mdvaldosta
Published on 2010-04-15T16:32:51Z Indexed on 2010/04/15 16:43 UTC
Read the original article Hit count: 736

Filed under:
|
|
|
|

// From my form

BindingSource bs = new BindingSource();

private void fillStudentGrid()
{
     bs.DataSource = Admin.GetStudents();
     dgViewStudents.DataSource = bs;
}

// From the Admin class

public static List<Student> GetStudents()

{
    DojoDBDataContext conn = new DojoDBDataContext();

    var query =
        (from s in conn.Students
         select new Student
         {
             ID = s.ID,
             FirstName = s.FirstName,
             LastName = s.LastName,
             Belt = s.Belt
         }).ToList();

    return query;
}

I'm trying to fill a datagridview control in Winforms, and I only want a few of the values. The code compiles, but throws a runtime error:

Explicit construction of entity type 'DojoManagement.Student' in query is not allowed.

Is there a way to get it working in this manner?

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-sql