LINQ 101例子

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.