LINQ 101例子

LINQ - Set Operators
Distinct去掉重复 Union(合并) Intersect(交集) Except叉集 Concat(连接)

LINQ - Restriction Operators
< > == && ||

LINQ - Query Execution
i++

LINQ - Quantifiers
Any集合中是否有元素满足某一条件 All集合中所有元素是否都满足某一条件
Contains集合中是否包含有某一元素 Exists是否存在

LINQ - Projection Operators
dt.AsEnumerable()可用于linq方法 n.Field<int>(column)

LINQ - Partitioning Operators
Take(3)前3个 Skip(4) 跳过前4个 TakeWhile(n => n < 6)不要后面6个 SkipWhile()指定条件跳过

LINQ - Ordering Operators
orderby word orderby word.Length orderby d descending倒叙 words.OrderBy()
words.OrderByDescending() orderby digit.Length, digit
words.OrderBy(a => a.Length).ThenBy(a => a, new CaseInsensitiveComparer());
words.OrderBy(a => a.Length).ThenByDescending(a => a, new CaseInsensitiveComparer());
.Reverse()翻转
public class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
}
}

LINQ - Miscellaneous Operators
Concat 连接 wordsA.SequenceEqual(wordsB);比较 是否相同

LINQ - Join Operators
join cust in customers on new { sup.City, sup.Country } equals new { cust.City, cust.Country
} into cs

LINQ - Generation Operators
var numbers =from n in Enumerable.Range(100, 50) 生成指定范围的整数数列
var numbers = Enumerable.Repeat(7, 10); 生成重复的序列号

LINQ - Element Operators
.First(); First(s => s[0] == 'o'); FirstOrDefault();
FirstOrDefault(p => p.ProductID == 789); ElementAt(1); 返回索引指定

LINQ - Custom Sequence Operators
public static IEnumerable<S> Combine<S>(this IEnumerable<DataRow> first,
IEnumerable<DataRow> second, System.Func<DataRow, DataRow, S> func)
{
using (IEnumerator<DataRow> e1 = first.GetEnumerator(), e2 =
second.GetEnumerator())
{
while (e1.MoveNext() && e2.MoveNext())
{
yield return func(e1.Current, e2.Current);
}
}
}

var numbersA = testDS.Tables["NumbersA"].AsEnumerable();
var numbersB = testDS.Tables["NumbersB"].AsEnumerable();

int dotProduct = numbersA.Combine(numbersB, (a, b) => a.Field<int>("number")
* b.Field<int>("number")).Sum();


LINQ - Conversion Operators
ToArray(); ToList(); ToDictionary(sr => sr.Name);
numbers.OfType<double>(); 根据指定类型筛选

LINQ - Aggregate Operators
Max Min SUN 要看的
LINQ - Grouping Operators

你可能感兴趣的:(LINQ)