Metodo di estensione per implementare l'Except

Scritto da  Alessandro Mostarda il venerdì 10 dicembre 2010  •  Linguaggio:    • 


Il seguente metodo di estensione implementa la funzione Except tra due liste con una funzione di confronto definita tramite lambda expression.

La funzione restituisce la differenza insiemistica tra due liste, ovvero permette di ottenere una lista di oggetti contenuti nella prima ma non nella seconda.

C#

public static IEnumerable<T> Except<T,C>(this IEnumerable<T> source,
IEnumerable<C> target,
Func<T, C, bool> compareCriteria)
{
return source.Where(x => !target.Any(y => compareCriteria(x, y)));
}

 

VB.NET

Imports System.Runtime.CompilerServices
Public Module Extension

<Extension()> _
Public Function Except(Of T, C)(ByVal source As IEnumerable(Of T), _
ByVal target As IEnumerable(Of C), _
ByVal compareCriteria As Func(Of T, C, Boolean)) As IEnumerable(Of T)
Return source.Where(Function(x) Not target.Any(Function(y) compareCriteria(x, y)))
End Function

End Module

 

Un esempio di utilizzo è il seguente:

C#

public void Main()
{
List<Person> firstList = new List<Person>();
firstList.Add(new Person {
Name = "Antonio",
Surname = "Verdi",
DateOfBirth = new System.DateTime(1975, 7, 22)
});
firstList.Add(new Person {
Name = "Mario",
Surname = "Rossi",
DateOfBirth = new System.DateTime(1956, 3, 14)
});
firstList.Add(new Person {
Name = "Giuseppe",
Surname = "Bianchi",
DateOfBirth = new System.DateTime(1985, 11, 25)
});

List<Person> secondList = new List<Person>();
secondList.Add(new Person {
Name = "Mario",
Surname = "Verdi",
DateOfBirth = new System.DateTime(1975, 7, 22)
});
secondList.Add(new Person {
Name = "Mario",
Surname = "Rossi",
DateOfBirth = new System.DateTime(1989, 2, 4)
});
secondList.Add(new Person {
Name = "Giuseppe",
Surname = "Verdi",
DateOfBirth = new System.DateTime(1985, 11, 14)
});

var mismatchBySurname = firstList.Except(secondList, (x, y) => x.Surname == y.Surname);
var mismatchByName = firstList.Except(secondList, (x, y) => x.Name == y.Name);
var mismatchByNameAndSurname = firstList.Except(secondList, (x, y) => x.Name == y.Name && x.Surname == y.Surname);
var mismatchByYear = firstList.Except(secondList, (x, y) => x.DateOfBirth.Year == y.DateOfBirth.Year);
}

public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public DateTime DateOfBirth { get; set; }
}

 

VB.NET

Sub Main()
Dim firstList As New List(Of Person)
firstList.Add(New Person With {.Name = "Antonio", .Surname = "Verdi", .DateOfBirth = New Date(1975, 7, 22)})
firstList.Add(New Person With {.Name = "Mario", .Surname = "Rossi", .DateOfBirth = New Date(1956, 3, 14)})
firstList.Add(New Person With {.Name = "Giuseppe", .Surname = "Bianchi", .DateOfBirth = New Date(1985, 11, 25)})

Dim secondList As New List(Of Person)
secondList.Add(New Person With {.Name = "Mario", .Surname = "Verdi", .DateOfBirth = New Date(1975, 7, 22)})
secondList.Add(New Person With {.Name = "Mario", .Surname = "Rossi", .DateOfBirth = New Date(1989, 2, 4)})
secondList.Add(New Person With {.Name = "Giuseppe", .Surname = "Verdi", .DateOfBirth = New Date(1985, 11, 14)})

Dim mismatchBySurname = firstList.Except(secondList, Function(x, y) x.Surname = y.Surname)
Dim mismatchByName = firstList.Except(secondList, Function(x, y) x.Name = y.Name)
Dim mismatchByNameAndSurname = firstList.Except(secondList, Function(x, y) x.Name = y.Name AndAlso x.Surname = y.Surname)
Dim mismatchByYear = firstList.Except(secondList, Function(x, y) x.DateOfBirth.Year = y.DateOfBirth.Year)
End Sub

Public Class Person
Public Property Name As String
Public Property Surname As String
Public Property DateOfBirth As DateTime
End Class


Tags: Linq

 
x