score:0

Accepted answer

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

        }

score:1

but anonymous type can't Serialize in WCF.

score:0

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.

score:2

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

Related Articles