Metodo di estensione per implementare Intersect

Scritto da  Alessandro Mostarda il mercoledì 22 dicembre 2010  •  Linguaggio: C#,VB   • Livello: 200


Il seguente metodo di estensione implementa la funzione Intersect tra due liste con una funzione di confronto definita tramite lambda expression. Il metodo ritorna solo le istanze della collection a cui è applicato il metodo stesso.

Tale metodo può essere paragonato alla JOIN di LINQ, con l'unica differenza che non è possibile selezionare il tipo di ritorno.

C#

public static IEnumerable<T> Intersect<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 Intersect(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) target.Any(Function(y) compareCriteria(x, y)))
End Function

End Module

 

La sintassi per utilizzarli è la 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 matchBySurname = firstList.Intersect(secondList, (x, y) => x.Surname == y.Surname);
var matchByName = firstList.Intersect(secondList, (x, y) => x.Name == y.Name);
var matchByNameAndSurname = firstList.Intersect(secondList, (x, y) => x.Name == y.Name && x.Surname == y.Surname);
var matchByYear = firstList.Intersect(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 matchBySurname = firstList.Intersect(secondList, Function(x, y) x.Surname = y.Surname)
Dim matchByName = firstList.Intersect(secondList, Function(x, y) x.Name = y.Name)
Dim matchByNameAndSurname = firstList.Intersect(secondList, Function(x, y) x.Name = y.Name AndAlso x.Surname = y.Surname)
Dim matchByYear = firstList.Intersect(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