2014-03-27 134 views
0

我有如下功能:删除从集合项实体框架

IEnumerable<Group> GetAllChildren(int parentID) 
{ 
    using (Entities db = new Entities()) 
    { 
     var result = (from x in db.Groups 
         where x.ParentID == parentID 
         select x).ToList(); 
     foreach (Group child in result.ToList()) 
     { 
      result.AddRange(GetAllChildren(child.GroupID)); 
     } 
     return result; 
    } 
} 

在上面的函数,如果我通过一组名字,我让所有的孩子们在所有的水平。 它按预期工作。

现在我的查询看起来是这样的:

GroupNamesWithCorrespondingEffects 
    = new ObservableCollection<GroupNameWithCorrespondingEffect> 
       (from g in db.Groups 
       select new GroupNameWithCorrespondingEffect 
          { 
           GroupID = g.GroupID, 
           GroupName = g.GroupName, 
           CorrespondingEffect = g.Master_Effects.Effect 
          } 
       ); 

上面的查询会给我所有的群体。

现在我想从GroupNamesWithCorrespondingEffects中删除group with id == 25的孩子的所有组。

我在第二个查询中尝试过.Remove(GetAllChildren(25))。但我得到以下错误。

Collection.Remove(GroupNameWithCorrespondingEffect)有一些无效参数。

回答

1

希望这有助于你:

var childs = GetAllChildren(25).ToList(); 
var childIDList = childs.select(u => u.GroupID).ToList(); 

GroupNamesWithCorrespondingEffects = GroupNamesWithCorrespondingEffects 
    .Where(u => !childIDList.Contains(u.GroupID)).ToList(); 
+0

感谢它完美的作品。 – Vishal