Mohsan
Mohsan

Reputation: 2509

LINQ ERROR : Explicit construction of entity type

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

Upvotes: 2

Views: 4852

Answers (4)

TompyNation
TompyNation

Reputation: 21

If you care about the performance, you should change the query to:

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

      GridView1.DataSource = user;
      GridView1.DataBind();
      }

Upvotes: 2

Chase
Chase

Reputation: 564

You can't construct an entity type in a query. MS thinks developers are too dumb and will confuse themselves (among other things). A quick, easy, and inefficient fix...

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

            GridView1.DataSource = user;

            GridView1.DataBind();

        }

I wouldn't recommend this for a product environment as it will load the entire table but I've used it for an internal app for personal use. I would recommend creating an overloaded model.

Upvotes: 0

h2o
h2o

Reputation:

but anonymous type can't Serialize in WCF.

Upvotes: 1

Ahmed
Ahmed

Reputation: 11403

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();

        }

Upvotes: 0

Related Questions