2016-05-07 28 views
0

刪除UNMATCH項目最近我遇到了我的工作問題,下面是源代碼,LINQ處理IEnumerable和從子實體

this.NameList = new ObservableCollection<Name>(); 

     List<Others> others1 = new List<Others>(); 
     others1.Add(new Others() { Company = "Company1", School = "School1" }); 
     others1.Add(new Others() { Company = "Company1", School = "School1" }); 
     others1.Add(new Others() { Company = "Company1", School = "School1" }); 

     List<Others> others2 = new List<Others>(); 
     others2.Add(new Others() { Company = "Company2", School = "School2" }); 
     others2.Add(new Others() { Company = "Company2", School = "School2" }); 
     others2.Add(new Others() { Company = "Company2", School = "School2" }); 

     List<Others> others3 = new List<Others>(); 
     others3.Add(new Others() { Company = "Company3", School = "School3" }); 
     others3.Add(new Others() { Company = "Company3", School = "School3" }); 
     others3.Add(new Others() { Company = "Company3", School = "School3" }); 

     this.NameList.Add(new Name() { FirstName = "Jacob", LastName = "Deng", Others = others1 }); 
     this.NameList.Add(new Name() { FirstName = "David", LastName = "Xu", Others = others2 }); 
     this.NameList.Add(new Name() { FirstName = "Helen", LastName = "Liu", Others = others3 }); 

please click here to see the output of above code

然後請閱讀下面的代碼,

List<Others> others1 = new List<Others>(); 
     others1.Add(new Others() { Company = "Company1", School = "School1" }); 
     others1.Add(new Others() { Company = "Company1", School = "School1" }); 
     others1.Add(new Others() { Company = "Company1", School = "School1" }); 

     List<Others> others2 = new List<Others>(); 
     List<Others> others3 = new List<Others>(); 

     this.NameList.Add(new Name() { FirstName = "Jacob", LastName = "Deng", Others = others1 }); 
     this.NameList.Add(new Name() { FirstName = "David", LastName = "Xu", Others = others2 }); 
     this.NameList.Add(new Name() { FirstName = "Helen", LastName = "Liu", Others = others3 }); 

Please click here for the output of the second snippet code

從上面的兩個片段中,您可能會注意到第二個片段不包含other2和other3中的任何項目,您可以從預覽輸出中輕鬆理解。

現在問題來了,我們如何使用LINQ to IEnumerable來處理這種情況,如何使用LINQ從其他實體中刪除這些項目?但我們需要保持other2和other3不爲空(保持計數爲0)。除了LINQ之外,還有其他解決方案嗎?

我嘗試使用以下,但失敗了,

var others = ((MyVM)DataContext).NameList.Select(n => n.Others.Where(o => o.School == "School1")).ToList(); 

我只是做了一些測試,如果我們不使用LINQ,那麼我們可以使用下面的代碼片段,以解決我的吧。

ObservableCollection<Name> nameList = ((MyVM)DataContext).NameList; 
     foreach(Name n in nameList) 
     { 
      List<Others> removeList = new List<Others>(); 
      for(int i=0;i<n.Others.Count;i++) 
      { 
       if(n.Others[i].School!="School1") 
       { 
        removeList.Add(n.Others[i]); 
       } 
      } 
      foreach(Others other in removeList) 
      { 
       n.Others.Remove(other); 
      } 
     } 

但它看起來很冗餘,我們知道LINQ是非常有用的,我相信它可以用LINQ修復。希望有人能幫我用LINQ修復它,謝謝。

欣賞有人能幫助我。非常感謝。

+0

試試這個列表:VAR他人=((MyVM)的DataContext).NameList.Select(N => n.Others.Select(鄰=>(o.School ==「School1」)?o:「在此處添加代碼以執行其他操作」))。ToList(); – jdweng

+0

謝謝Jdweng,我只是想刪除那些學校不等於'學校1'LINQ的項目,並且輸出與上面提到的第二個片段代碼相同。你可以幫幫我嗎? – Jacob

+0

這意味着當o.School!='School1',然後從其他實體中刪除該項目 – Jacob

回答

2

最好在你的情況下使用List<T>.RemoveAll。有關更多詳細信息,請參見Using LINQ to remove elements from a List<T>

ObservableCollection<Name> nameList = ((MyVM)DataContext).NameList; 
foreach(Name n in nameList) 
{ 
    n.Others.RemoveAll(s => s.School != "School1"); 
} 

Linq擅長只讀迭代而不是更新/刪除。如果你堅持使用Linq,你必須重新構建每個項目。

var newList = from nl in this.namelist 
       // this list can be empty but never null 
       let notSchool1 = (from s in nl.Others 
            where s.School != "School1" 
            select s).ToList() 
       select new Name 
       { 
        FirstName = nl.FirstName, 
        LastName = nl.LastName, 
        Others = noSchool1 
       }; 
this.NameList = new ObservableCollection<Name>(newList); 

上面的代碼可能會破壞其他功能在你的應用程序,因爲這些項目是觀察

+0

非常感謝您的幫助。非常漂亮的代碼! @qxg – Jacob

0

據我瞭解你的問題, 你想刪除其他學校的價值不是'學校1'。有不同的方式可用,但我建議從NameList過濾數據,你想要的。

你可以試試下面的代碼讓別人用 'School1'

var result = NameList.Select(name => new Name() 
      { 
       FirstName = name.FirstName, 
       LastName = name.LastName, 
       Others = name.Others.Where(obj => obj.School == "School1").ToList() 
      } 
      ).ToList();