• How to extend IEnumable<T> ?


    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 { setget; }
            public string ProductName { setget; }
            public string Description { setget; }
        }

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


  • 相关阅读:
    centos下如何关闭xdebug扩展
    xdebug调试的原理
    取值再拼接跳转链接
    描点返回-动画
    比较旧的写法:验证车牌、手机号、电话、qq等
    jQuery实现ie浏览器兼容placeholder效果
    CSS3的REM设置字体大小
    整理前端问题
    css3 hover效果
    ie7 a标签强制不换行兼容问题
  • 原文地址:https://www.cnblogs.com/Langzi127/p/2236468.html
Copyright © 2020-2023  润新知