Here is a example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LamdaExpressionTest
{
class Program
{
static void Main(string[] args)
{
var list = new List<Product>
{
new Product{
ProductID = 1,
ProductName = "aaaa",
Description = "this is aaaa"
},
new Product{
ProductID = 2,
ProductName = "bbbb",
Description = "this is bbbb"
},
new Product{
ProductID = 3,
ProductName = "cccc",
Description = "this is cccc"
},
new Product{
ProductID = 4,
ProductName = "dddd",
Description = "this is dddd"
},
new Product{
ProductID = 5,
ProductName = "eeee",
Description = "this is eeee"
},
new Product{
ProductID = 6,
ProductName = "ffff",
Description = "this is ffff"
},
};
Func<Product, bool> fun = (Product product) =>
{
return product.ProductID % 2 == 0;
};
var list2 = list.Where(fun).ToList();
var list3 = list.FilterByHashCode().ToList();
list.ForEach(item => Console.WriteLine(item.ProductName));
}
}
public class Product
{
public int ProductID { set; get; }
public string ProductName { set; get; }
public string Description { set; get; }
}
public static class IEnumableExtension
{
public static IEnumerable<T> FilterByHashCode<T>(this IEnumerable<T> list)
{
if (list == null || list.Count() < 1)
{
yield break;
}
foreach (var item in list)
{
if (item.GetHashCode() % 2 == 0)
{
yield return item;
}
}
}
public static void Foreach<T>(this IEnumerable<T> colection, Action<T> action)
{
if(colection == null || colection.Count() < 1)
return;
foreach(var item in colection)
{
action(item);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LamdaExpressionTest
{
class Program
{
static void Main(string[] args)
{
var list = new List<Product>
{
new Product{
ProductID = 1,
ProductName = "aaaa",
Description = "this is aaaa"
},
new Product{
ProductID = 2,
ProductName = "bbbb",
Description = "this is bbbb"
},
new Product{
ProductID = 3,
ProductName = "cccc",
Description = "this is cccc"
},
new Product{
ProductID = 4,
ProductName = "dddd",
Description = "this is dddd"
},
new Product{
ProductID = 5,
ProductName = "eeee",
Description = "this is eeee"
},
new Product{
ProductID = 6,
ProductName = "ffff",
Description = "this is ffff"
},
};
Func<Product, bool> fun = (Product product) =>
{
return product.ProductID % 2 == 0;
};
var list2 = list.Where(fun).ToList();
var list3 = list.FilterByHashCode().ToList();
list.ForEach(item => Console.WriteLine(item.ProductName));
}
}
public class Product
{
public int ProductID { set; get; }
public string ProductName { set; get; }
public string Description { set; get; }
}
public static class IEnumableExtension
{
public static IEnumerable<T> FilterByHashCode<T>(this IEnumerable<T> list)
{
if (list == null || list.Count() < 1)
{
yield break;
}
foreach (var item in list)
{
if (item.GetHashCode() % 2 == 0)
{
yield return item;
}
}
}
public static void Foreach<T>(this IEnumerable<T> colection, Action<T> action)
{
if(colection == null || colection.Count() < 1)
return;
foreach(var item in colection)
{
action(item);
}
}
}
}