2010-07-19 5 views
2

Je suis un noob LINQ .... quelqu'un peut me s'il vous plaît comment y parvenir en utilisant LINQ certains ... Je suis en train de comparer les 2 listes dans les deux sens ...Comparer 2 listes à l'aide 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); 
    } 

Répondre

0

conversion Votre question initiale à une exigence anglais:

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 
     } 
    } 

, vous voulez donc faire:

//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; }); 

Assurez-vous que votre objet UserTeam a appropriées overrides pour les méthodes Equals et GetHashCode, afin que la comparaison entre deux UserTeams soit précise :)

0

Vous utiliseriez normalement Enumerable.Except dans les deux sens pour obtenir les différences. Puis ajoutez et supprimez au besoin.

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

Vous essayez d'obtenir les parties externes à partir d'une jointure externe complète. Voici un moyen approximatif d'y parvenir.

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(); 
     } 
    } 

}