Q:

Linq: get a custom class joining to tables

I'm developing a WCF RESTful service with C#, .NET Framework 4.0 and Entity Framework Code First.
I have this class:
[DataContract]
public class Group
{
    [DataMember]
    public int GroupId { get; set; }

    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public String Description { get; set; }

    [DataMember]
    public String City { get; set; }

    [DataMember]
    public String Country { get; set; }

    public byte[] Photo { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

And this class:
public class User
{
    [DataMember]
    public int UserId { get; set; }

    [DataMember]
    public string Name { get; set; }

    public virtual ICollection<Group> Groups { get; set; }
}

I want to retrieve a group using its GroupId and all its members (as an array of UserId).
Now, I have this sentence:
var groups = from g in context.Groups
where g.GroupId == groupId
select g;

But I don't know how can I get member's UserId.
I want to get a custom class like this one:
[DataContract]
public class CustomGroup
{
    [DataMember]
    public int GroupId { get; set; }

    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public String Description { get; set; }

    [DataMember]
    public String City { get; set; }

    [DataMember]
    public String Country { get; set; }

    [DataMember]
    public ICollection<int> UsersIds { get; set; }
}

How can I do that?

A:

var result = context.Groups.Where(g=>g.GroupId == groupId)
                    .Select(e=> new CustomGroup(e));
//With your CustomGroup class should looks like this:

[DataContract]
public class CustomGroup  //You should consider some inheritance relationship here
   public CustomGroup(Group g){
      GroupId = g.GroupId;
      Name = g.Name;
      Description = g.Description;
      City = g.City;
      Country = g.Country;
      UsersIds = g.Users.Select(u=>u.UserId).ToList();
   }
   //....  
   [DataMember]
   public ICollection<int> UsersIds { get; set; }
}

