码迷,mamicode.com
首页 > 其他好文 > 详细

委托是怎么最终变成linq表达式的?(下)

时间:2014-09-09 11:12:58      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   for   数据   art   

扩展方式(Extension Method)是给那些不是你拥有的,因而不能直接修改的类添加方法的一种方便的方法。首先我们定义一个类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rocky
{
   public  class Product
    {

       public int ProductID { set; get; }
       public string Name { set; get; }
       public string Description { set; get; }
       public decimal Price { set; get; }
       public string Category { set; get; }
    }
}

上述定义了一个组织数据的实体类  接下来我们定义一个购物车类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rocky
{
    public class ShoppingCar
    {

        public List<Product> Products { set; get; }
    }
}

这个购物车类型里面只有一个字段就是商品的集合Porducts 现在我们需要求出商品价格的总值,但我们不能对这个类进行修改。那么我们可以利用扩展方法来完成。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rocky
{
   public   static class MyExtensionMethod  //public修饰  静态类
    {

       public static decimal TotalPrices(this ShoppingCar car)//静态方法  第一个参数必须是this修饰要扩展的类名
       {
           decimal total = 0;
           foreach (var item in car.Products)
           {
               total += item.Price;
           }

          return  total;
       }
    }
}

最终我们看一下运行起来的结果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rocky
{
    class Program
    {
        static void Main(string[] args)
        {
            ShoppingCar cart = new ShoppingCar
            {
                Products = new List<Product> {
                    new Product{Name="kaka", Price=234M},
                    new Product{Name="Messic", Price=211M},
                    new Product{Name="lisi",Price=12M}
 
                }
            };

            decimal total = cart.TotalPrices();

            Console.WriteLine("Total:{0}",total);
            Console.ReadKey();
        }
    }
}

对接口运用扩展方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rocky
{
    public class ShoppingCar:IEnumerable<Product>
    {

        public List<Product> Products { set; get; }

      public   IEnumerator<Product> GetEnumerator()
        {
            return Products.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}

扩展接口方法

   public static decimal TotalPrices(this IEnumerable<Product> prods)
       {
           decimal total = 0;
           foreach (var item in prods)
           {
               total += item.Price;
           }

           return total;
          
       }

结果是

   IEnumerable<Product> cart = new ShoppingCar
            {
                Products = new List<Product> {
                    new Product{Name="kaka", Price=234M},
                    new Product{Name="Messic", Price=211M},
                    new Product{Name="lisi",Price=12M}
 
                }
            };

            

            Console.WriteLine("Total:{0}",cart.TotalPrices());
            Console.ReadKey();
        }

 通过扩展方法结果可以看出接口运用扩展方法。

  最后我们要演示的一种扩展方法是,它可以对对象集合进行过滤

委托是怎么最终变成linq表达式的?(下)

标签:des   style   blog   color   io   ar   for   数据   art   

原文地址:http://www.cnblogs.com/renyanlei/p/3936101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!