tags:

views:

1411

answers:

2

hi.. i am getting this error at GridView1.DataBind();

Explicit construction of entity type 'WebApplication1.MUser' in query is not allowed.

 using (var db = new UsersDataContext())
            {
                IEnumerable<MUser> user = from u in db.MUsers
                                          where u.Id == 1
                                          select new MUser {Username = u.Username, Id = u.Id, Password=u.Password, ProjectUsers=u.ProjectUsers };

                GridView1.DataSource = user;

                GridView1.DataBind();

            }

how to solve this?

i am following this tutorial http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx

A: 

If you want to return the full Muser object just use:

using (var db = new UsersDataContext())
        {
            IEnumerable<MUser> user = from u in db.MUsers
                                      where u.Id == 1
                                      select u;

            GridView1.DataSource = user;

            GridView1.DataBind();

        }

alternatively, if you want to return a custom part of the user (i.e. only some properties from the Muser object) you can use anonymous type as follows.

using (var db = new UsersDataContext())
        {
            IEnumerable<MUser> user = from u in db.MUsers
                                      where u.Id == 1
                                      select new  {Username = u.Username, Id = u.Id, Password=u.Password, ProjectUsers=u.ProjectUsers };

            GridView1.DataSource = user;

            GridView1.DataBind();

        }
Galilyou
+1  A: 

but anonymous type can't Serialize in WCF.