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

Linq101-CustomSequence

时间:2015-05-12 18:39:35      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 
 5 namespace Linq101
 6 {
 7     class CustomSequence
 8     {
 9         public void Linq98()
10         {
11             int[] vectorA = { 0, 2, 4, 5, 6 };
12             int[] vectorB = { 1, 3, 5, 7, 8 };
13 
14             int result = vectorA.Combine(vectorB, (a, b) => a * b).Sum();
15             Console.WriteLine(result);
16         }
17     }
18 
19     public static class CustomSequenceOperators
20     {
21         public static IEnumerable<int> Combine(this IEnumerable<int> first, IEnumerable<int> second, Func<int, int, int> func)
22         {
23             //List<int> list=new List<int>();
24             using (IEnumerator<int> e1 = first.GetEnumerator(), e2 = second.GetEnumerator())
25             {
26                 while (e1.MoveNext() && e2.MoveNext())
27                 {
28                     yield return func(e1.Current, e2.Current);
29                     //list.Add(func(e1.Current, e2.Current));
30                 }
31             }
32             //return list;
33         }
34     }
35 }

 

Linq101-CustomSequence

标签:

原文地址:http://www.cnblogs.com/David-Huang/p/4498043.html

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