2014-03-27 132 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