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

转载于:https://www.cnblogs.com/jazzka702/archive/2012/12/25/2832536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值