2010-07-19 141 views
2

我是LINQ小白2只列出了....有人可以請一些我如何實現這一目標使用LINQ ......我想在兩個方向上比較2所列出...比較使用LINQ

internal void UpdateUserTeams(int iUserID) 
    { 
     UserTeamCollection CurrentTeams = GetUserTeams(iUserID); 
     UserTeamCollection UpdatedTeams = this; 

     foreach (UserTeam ut in CurrentTeams) 
     { 
      if(!UpdatedTeams.ContainsTeam(ut.ID)) 
      { 
       RemoveTeamFromDB(); 
      } 
     } 

     foreach (UserTeam ut in UpdatedTeams) 
     { 
      if (!CurrentTeams.ContainsTeam(ut.ID)) 
      { 
       AddTeamToDB(); 
      } 
     } 

    } 


    public bool ContainsTeam(int iTeamID) 
    { 
     return this.Any(t => t.ID == iTeamID); 
    } 

回答

0

所以你最初的問題轉換爲英語要求:

foreach (UserTeam ut in CurrentTeams)   // for each current team 
    { 
     if(!UpdatedTeams.ContainsTeam(ut.ID))  // that is not in the updated teams list 
     { 
      RemoveTeamFromDB();   // remove it from the database 
     } 
    } 

    foreach (UserTeam ut in UpdatedTeams)  //for each of the teams in the updated teams list 
    { 
     if (!CurrentTeams.ContainsTeam(ut.ID)) //if the current team does not contain the updated team 
     { 
      AddTeamToDB();    //add the team to the database 
     } 
    } 

因此,你想做的事:

//select all current teams that are not in updated teams list 
CurrentTeam.Except(UpdatedTeams).All(team => { RemoveTeamFromDB(team); return true; }); 

//select all updated teams that are not in the current teams list 
UpdatedTeam.Except(CurrentTeams).All(team => { AddTeamToDB(team); return true; }); 

確保您UserTeam對象有適當覆蓋Equals和GetHashCode方法,以便兩個UserTeams之間的比較是準確的:)

0

您通常會使用Enumerable.Except這兩種方式來獲得差異。然後根據需要添加和刪除。

0
var addedTeams = UpdatedTeams.Except(CurrentTeams); 
var removedTeams = CurrentTeams.Except(UpdatedTeams); 
0

您試圖從完整的外部連接中獲取外部零件。這是實現這一目標的一個粗略方法。

ILookup<int, UserTeam> currentLookup = CurrentTeams 
    .ToLookup(ut => ut.ID); 
ILookup<int, UserTeam> updatedLookup = UpdatedTeams 
    .ToLookup(ut => ut.ID); 

List<int> teamIds = CurrentTeams.Select(ut => ut.ID) 
    .Concat(UpdatedTeams.Select(ut => ut.ID)) 
    .Distinct() 
    .ToList(); 

ILookup<string, UserTeam> results = 
(
    from id in teamIds 
    let inCurrent = currentLookup[id].Any() 
    let inUpdated = updatedLookup[id].Any() 
    let key = inCurrent && inUpdated ? "No Change" : 
    inCurrent ? "Remove" : 
    inUpdated ? "Add" : 
    "Inconceivable" 
    let teams = key == "Remove" ? currentLookup[id] : 
    updatedLookup[id] 
    from team in teams 
    select new {Key = key, Team = team) 
).ToLookup(x => x.Key, x => x.Team); 

foreach(UserTeam ut in results["Remove"]) 
{ 
    RemoveTeamFromDB(); 
} 
foreach(UserTeam ut in results["Add"]) 
{ 
    AddTeamToDB(); 
} 
2
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text;  

namespace Linqage 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      UserTeamCollection currentTeams = new UserTeamCollection() 
      { 
       new UserTeam(1), 
       new UserTeam(2), 
       new UserTeam(3), 
       new UserTeam(4), 
       new UserTeam(5) 
      }; 

      UserTeamCollection updatedTeams = new UserTeamCollection() 
      { 
       new UserTeam(2), 
       new UserTeam(4), 
       new UserTeam(6), 
       new UserTeam(8)     
      }; 

      currentTeams.Except(updatedTeams).All(u => 
      { 
       //Console.WriteLine("Item ID: {0}",u.ID); 
       //RemoveFromDB() 
       return true; 
      }); 

      updatedTeams.Except(currentTeams).All(u => 
      { 
       //Console.WriteLine("Item ID: {0}", u.ID); 
       //AddToDB() 
       return true; 
      });    
     } 
    } 

    public class UserTeamCollection 
     : List<UserTeam> 
    { 

    } 

    //Either overwrite the GetHashCode and Equals method OR create a IComparer 
    public class UserTeam 
    { 
     public int ID { get; set; } 

     public UserTeam(int id) 
     { 
      ID = id; 
     } 
     public override bool Equals(object obj) 
     { 
      UserTeam iOther = obj as UserTeam; 
      if (iOther != null) 
      { 
       return this.ID == iOther.ID; 
      } 
      return false; 
     } 
     public override int GetHashCode() 
     { 
      return ID.GetHashCode(); 
     } 
    } 

}