2012-01-07 127 views
0

我有一個名爲CandidateListOfMyObjects的對象MyObjects的列表和名爲InitialListOfMyObjects的另一個列表。我正在寫一個方法,用linq更新,刪除或添加MyObjects的實例到列表中?現在用linq添加/更新/刪除列表中的項目

MyObject ThisObject = new MyObject(); //MyObject has a field call ObjectID 

for (int index = 0; index < ListOfMyObjects.Count; index++) 
{ 
    ThisObject = null; 
    ThisObject = ListOfMyObjects[index]; 

    if (condition) 
    { 
    add ThisObject to InitialListOfMyObjects 
    } 

    if (condition) 
    { 
    replace the MyObject with ID ThisObject.ID in the InitialListOfMyObjects with ThisObject 
    } 

    if (condition) 
    { 
    delete the ThisObject with ID ThisObject.ID from the InitialListOfMyObjects 
    } 
} 

因爲,我循環通過InitialListOfMyObjects但我在看我的代碼,我在做內循環循環,所以我敢肯定這不是:

這一個工作循環內最好的方式來做到這一點。

感謝您的建議。

+2

LINQ被設計成避免副作用(它來自函數編程)將數據添加到列表中。換句話說,它不是爲了改變原始列表而設計的。相反,它可以創建並返回一個新列表。 – TrueWill 2012-01-07 17:09:29

+0

所以我不能用linq做到這一點?我必須讓循環內循環? – frenchie 2012-01-07 17:17:54

+1

完全沒有。您可以使用Linq方法調用的結果來替換您的列表。 – sq33G 2012-01-07 17:41:27

回答

0

我不知道你的condition是什麼樣子,但我認爲,僅僅用更新的列表取代了原來的名單是不夠的。這是我的兩個列表之間的Linq同步版本。

//replace original with synchronized list 
original = from updatedPair in updated 
      join originalPair in original 
//group join the updated to the original: 
//all updates will be included, 
//but only those originals with a matching update 
//(deletes items not in the updated list automatically) 
      on updatedPair.Key equals originalPair.Key 
      into originalsForUpdate 
//added items will have a null originalForUpdate 
      let originalForUpdate = originalsForUpdate.DefaultIfEmpty().Single() 
      where !ConditionForDelete(updatedPair, originalForUpdate) 
      select new MyObjectThingy 
      { updatedPair.Key, 
       ConditionForUpdate(originalForUpdate,updatedPair) ? 
       updatedPair.Value : 
       originalForUpdate.Value 
      }.ToList(); 

注意的是,雖然這允許在添加或更新條件,但對僅刪除限制條件 - 任何只是在原來的列表中,但不是在最新的名單將不會在結果集。要解決此問題,最後請致電ToList,然後致電Union(original.Where(item => !updated.Any(updItem => updItem.Key == item.Key) && ConditionForUndoingDelete(item)))

+0

好的,非常感謝,非常酷。 – frenchie 2012-01-08 15:23:22

0

您可以創建自己的擴展方法。將此方法置於公共靜態類中:

public static void ForEach<T>(this IEnumerable<T> values, Action<T> action) 
{ 
    foreach (var value in values) 
     action(value); 
} 

它使您能夠對LINQ樣式中的每個可枚舉元素執行操作。

articles 
    .Where(a => a.Price < 100.0) 
    .ForEach(a => { a.Price *= 1.2; a.Updated = true; }); 
+0

我聽說靜態類有些危險。如果兩個或兩個以上的人在相同的毫微秒EXACT調用擴展方法,這是如何工作的?有沒有辦法做到這一點沒有擴展方法? – frenchie 2012-01-07 20:05:58

+2

從我所瞭解的靜態函數可以很好地使用,它是靜態變量,如果在同一時間訪問/修改,它可能是危險的,並會導致衝突。 – ToddBFisher 2012-01-07 20:13:02

+1

擴展方法需要靜態類。如果兩個人同時調用擴展方法,*如果擴展方法不使用靜態數據*,則不會發生任何錯誤。如果共享數據,則會發生線程衝突。 – Amy 2012-01-07 20:13:31

1

對於使用LINQ

public void insertintolist() 
    { 

     var accountService = new AccountServicetest(); 
     var storethread = new Thread(); 
     storethread.TrdTopic = "JavaScript"; 
     storethread.EmailID = "[email protected]"; 
     storethread.Name = "nived"; 
     storethread.Topic = "Function"; 
     storethread.Message = "what is a functio "; 
     storethread.Posts = 0; 
     storethread.Views = 0; 
     accountService.StoreThread(storethread); 

    } 

    public void StoreThread(Thread model) 
    { 

     if (model != null) 
     { 
      model.TrdTopicID = 4; 
      accountcontrollertest.storethread.Add(model); 
     } 

    } 
相關問題