日期:2014-05-20  浏览次数:20952 次

使用linq为什么缺少一些方法
我在看网上使用LINQ的例子,但我的VS2008却没有那些方法,比如:
string[].EqualAll(string[])//没有EqualAll
int[].Fold()//没有Fold
像where,orderby这些是有的,为什么会缺少一些方法?
需要安装什么插件吗?

------解决方案--------------------
不过,你改成如下,可能稍接近于它要表达的目的,除了Sum()操作以外,

下面这段测试过了。

C# code
static class Program
{
        static void Main(string[] args)
        {
            LinqTest();
        }
        public static void LinqTest()
        {
            int[] vectorA = { 0, 2, 4, 5, 6 };
            int[] vectorB = { 1, 3, 5, 7, 8 };
            foreach (var num in vectorA.Combine(vectorB, (a, b) => a * b))
            {
                Console.WriteLine("Dot product: {0}", num);
            }
            Console.ReadLine();
        }
        public static IEnumerable<int> Combine(this IEnumerable<int> first, IEnumerable<int> second, Func<int, int, int> func)
        {
            var e1 = first.GetEnumerator();
            var e2 = second.GetEnumerator();
            
            while (e1.MoveNext() && e2.MoveNext())
                yield return func(Convert.ToInt32(e1.Current), Convert.ToInt32(e2.Current));
            
        }
}