101个LINQ例子(6)

2019-03-29 11:04

var wordsA = new string[] { \ var wordsB = new string[] { \ bool match = wordsA.EqualAll(wordsB);

Console.WriteLine(\}

EqualAll - 2

public void Linq97() {

var wordsA = new string[] { \ var wordsB = new string[] { \ bool match = wordsA.EqualAll(wordsB);

Console.WriteLine(\}

Custom Sequence Operators Combine

public static class CustomSequenceOperators {

public static IEnumerable Combine(this IEnumerable first, IEnumerable second, Func func) { using (IEnumerator e1 = first.GetEnumerator(), e2 = second.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { yield return func(e1.Current, e2.Current); } } }

}

public void Linq98() {

int[] vectorA = { 0, 2, 4, 5, 6 }; int[] vectorB = { 1, 3, 5, 7, 8 };

int dotProduct = vectorA.Combine(vectorB, (a, b) => a * b).Sum(); Console.WriteLine(\ }

Query Execution Deferred

public void Linq99() {

// Sequence operators form first-class queries that // are not executed until you enumerate over them. int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int i = 0; var q =

from n in numbers select ++i;

// Note, the local variable 'i' is not incremented

// until each element is evaluated (as a side-effect): foreach (var v in q) {

Console.WriteLine(\ } }

Immediate

public void Linq100() {

// Methods like ToList() cause the query to be // executed immediately, caching the results.

int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int i = 0;

var q = (

from n in numbers select ++i ) .ToList();

// The local variable i has already been fully // incremented before we iterate the results: foreach (var v in q) {

Console.WriteLine(\ }

}

Query Reuse

public void Linq101() {

// Deferred execution lets us define a query once // and then reuse it later after data changes. int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNumbers =

from n in numbers where n <= 3 select n;

Console.WriteLine(\ foreach (int n in lowNumbers) { Console.WriteLine(n); }

for (int i = 0; i < 10; i++) { numbers[i] = -numbers[i]; }

// During this second run, the same query object, // lowNumbers, will be iterating over the new state // of numbers[], producing different results: Console.WriteLine(\ foreach (int n in lowNumbers) {

Console.WriteLine(n); } }


101个LINQ例子(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:海韵嘉园业务资料

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: