Search code examples
c#linqiqueryableentitycollection

How to convert IQueryable to EntityCollection


I really cannot find the answer about how to convert IQueryable to EntityCollection?

var userGroups = this.ObjectContext.UserGroups;

 foreach (var userGroup in userGroups )
 {
     var ussers = this.ObjectContext.Users.Where(f => f.UserGroupID == item.ID && f.IsActive == true);

     userGroup.Users = users; // Here I' am getting an issue.
 }

Solution

  • I'm not 100% sure because you don't specify, but I'm guessing that your userGroup.Users property is a navigation property generated by the EDMX model, so the entity collection is already created for you, all you need to do is add your users to the collection.

    var userGroups = this.ObjectContext.UserGroups;
    
     foreach (var userGroup in userGroups )
     {
         var ussers = this.ObjectContext.Users.Where(f => f.UserGroupID == item.ID && f.IsActive == true);
    
         foreach(var user in ussers)
            userGroup.Users.Add(user);
     }