-
linq 常用的语法
//Copyright (C) Microsoft Corporation. All rights reserved.
![](/Images/OutliningIndicators/None.gif)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
static class Samples
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static int[] numbers = new []
{ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static string[] strings = new []
{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
class Person
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{get; set;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Level
{get; set;}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static Person[] persons = new Person[]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Person
{Name="Jesper", Level=3},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Person
{Name="Lene", Level=3},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Person
{Name="Jonathan", Level=5},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Person
{Name="Sagiv", Level=3},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Person
{Name="Jacqueline", Level=3},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Person
{Name="Ellen", Level=3},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Person
{Name="Gustavo", Level=9}
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample1()
{
// use Where() to filter out elements matching a particular condition
var fnums = numbers.Where(n => n < 5);
Console.WriteLine("Numbers < 5");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(int x in fnums)
{
Console.WriteLine(x);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample2()
{
// use First() to find the one element matching a particular condition
string v = strings.First(s => s[1]== 'e');
Console.WriteLine("string starting with 'o': {0}", v);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample3()
{
// use Select() to convert each element into a new value
var snums = numbers.Select(n => strings[n]);
Console.WriteLine("Numbers");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(string s in snums)
{
Console.WriteLine(s);
}
}
public static void Sample4()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// use Anonymous Type constructors to construct multi-valued results on the fly
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var q = strings.Select(s => new
{Head = s.Substring(0,1), Tail = s.Substring(1)});
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var p in q)
{
Console.WriteLine("Head = {0}, Tail = {1}", p.Head, p.Tail);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample5()
{
// Combine Select() and Where() to make a complete query
var q = numbers.Where(n => n < 5).Select(n => strings[n]);
Console.WriteLine("Numbers < 5");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var x in q)
{
Console.WriteLine(x);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample6()
{
// Sequence operators form first-class queries are not executed until you enumerate them.
int i = 0;
var q = numbers.Select(n => ++i);
// Note, the local variable 'i' is not incremented until each element is evaluated (as a side-effect).
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var v in q)
{
Console.WriteLine("v = {0}, i = {1}", v, i);
}
Console.WriteLine();
// Methods like ToList() cause the query to be executed immediately, caching the results
int i2 = 0;
var q2 = numbers.Select(n => ++i2).ToList();
// The local variable i2 has already been fully incremented before we iterate the results
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var v in q2)
{
Console.WriteLine("v = {0}, i2 = {1}", v, i2);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample7()
{
// use GroupBy() to construct group partitions out of similar elements
var q = strings.GroupBy(s => s[0]); // <- group by first character of each string
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var g in q)
{
Console.WriteLine("Group: {0}", g.Key);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(string v in g)
{
Console.WriteLine("\tValue: {0}", v);
}
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample8()
{
// use GroupBy() and aggregates such as Count(), Min(), Max(), Sum(), Average() to compute values over a partition
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var q = strings.GroupBy(s => s[0]).Select(g => new
{FirstChar = g.Key, Count = g.Count()});
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var v in q)
{
Console.WriteLine("There are {0} string(s) starting with the letter {1}", v.Count, v.FirstChar);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample9()
{
// use OrderBy()/OrderByDescending() to give order to your resulting sequence
var q = strings.OrderByDescending(s => s); // order the strings by their name
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(string s in q)
{
Console.WriteLine(s);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample9a()
{
// use ThenBy()/ThenByDescending() to provide additional ordering detail
var q = persons.OrderBy(p => p.Name).ThenBy(p => p.Level);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var p in q)
{
Console.WriteLine("{0} {1}", p.Level, p.Name);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Sample10()
{
// use query expressions to simplify
var q = from p in persons
orderby p.Level, p.Name
group p.Name by p.Level into g
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{Level = g.Key, Persons = g};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var g in q)
{
Console.WriteLine("Level: {0}", g.Level);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach(var p in g.Persons)
{
Console.WriteLine("Person: {0}", p);
}
}
}
}
![](/Images/OutliningIndicators/None.gif)
-
相关阅读:
html5中新增的form表单属性
FORM
.Net Core 发布失败
Sql Server查询最近执行sql
HttpWebRequest.GetResponse()操作超时
使用SqlBulkCopy批量插入/更新数据
ADO .NET 往数据库批量插入数据发生错误:超时时间已到,但是尚未从池中获取链接
Ueditor代码内容前台只显示一行
Lambda表达式
委托的简单使用
-
原文地址:https://www.cnblogs.com/gwazy/p/995761.html
Copyright © 2020-2023
润新知