Intersect - 2

Query
public void Linq51()
{
List products = GetProductList();
List customers = GetCustomerList();

var productFirstChars =
from p in products
select p.ProductName[0];
var customerFirstChars =
from c in customers
select c.CompanyName[0];

var commonFirstChars = productFirstChars.Intersect(customerFirstChars);

Console.WriteLine("Common first letters from Product names and Customer names:");
foreach (var ch in commonFirstChars)
{
Console.WriteLine(ch);
}
}
Lambda Expression
public void Linq51()
{
List products = GetProductList();
List customers = GetCustomerList();

var productFirstChars = products.Select(prod => prod.ProductName[0]);
var customerFirstChars = customers.Select(cust => cust.CompanyName[0]);

var commonFirstChars = productFirstChars.Intersect(customerFirstChars);

Console.WriteLine("Common first letters from Product names and Customer names:");
foreach (var ch in commonFirstChars)
{
Console.WriteLine(ch);
}
}
Output
Common first letters from Product names and Customer names:
C
A
G
N
M
I
Q
K
T
P
S
R
B
V
F
E
W
L
O
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +