2

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.
 }
NoWar
  • 36,338
  • 80
  • 323
  • 498

1 Answers1

3

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);
 }
CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • So there is no LINQ/ Helper class/ Something to do it without FOREACH ? – NoWar Apr 18 '12 at 19:11
  • 2
    LINQ is specifically designed to not have side effects, it is for querying. What you're doing here is causing side effects, so it shouldn't be in an expansion method, it *should* be in a foreach. – Servy Apr 18 '12 at 19:11
  • 1
    Not that I am aware of, keep in mind than an IQueryable can be returned from _lots_ of different sources, it may not always contain entities from the entity framework. You could of course easily create your own extension method that does the work for you. – CodingGorilla Apr 18 '12 at 19:12
  • 2
    @ClarkKent If you prefer, you can use: `users.ForEach` – Asad Saeeduddin Apr 11 '14 at 12:02