Linq To Object 查询语句示例

来源:互联网 发布:数据汇聚层的作用 编辑:程序博客网 时间:2024/05/16 12:21
var query = from employee in employeeswhere employee.Salary > 100000orderby employee.LastName, employee.FirstNameselect new { LastName = employee.LastName,FirstName = employee.FirstName };Console.WriteLine( "Highly paid employees:" );foreach( var item in query ) {Console.WriteLine( "{0}, {1}",item.LastName,item.FirstName );


用lamba表达式代替上面的效果

var query = employees.Where( emp => emp.Salary > 100000 ).OrderBy( emp => emp.LastName ).OrderBy( emp => emp.FirstName ).Select( emp => new {LastName = emp.LastName,FirstName = emp.FirstName} );


使用Enumerable实现上述功能

var query =Enumerable.Select(Enumerable.OrderBy(Enumerable.OrderBy(Enumerable.Where(employees, emp => emp.Salary > 100000),emp => emp.LastName ),emp => emp.FirstName ),emp => new {LastName = emp.LastName,FirstName = emp.FirstName} );

linq枚举数字,并对对象赋值

using System;using System.Linq;public class MultTable{static void Main() {var query = from x in Enumerable.Range(0,10)from y in Enumerable.Range(0,10)select new {X = x,Y = y,Product = x * y};foreach( var item in query ) {Console.WriteLine( "{0} * {1} = {2}",item.X,item.Y,item.Product );}}}

在数组对象中添加值并使用linq

public class NonGenericLinq{static void Main() {ArrayList numbers = new ArrayList();numbers.Add( 1 );numbers.Add( 2 );var query = from int n in numbersselect n * 2;foreach( var item in query ) {Console.WriteLine( item );}}}

在使用join语句的情况下使用linq

using System;using System.Linq;using System.Collections.Generic;public class EmployeeId{public string Id { get; set; }public string Name { get; set; }}public class EmployeeNationality{public string Id { get; set; }public string Nationality { get; set; }}public class JoinExample{static void Main() {// Build employee collectionvar employees = new List<EmployeeId>() {new EmployeeId{ Id = "111-11-1111",Name = "Ed Glasser" },new EmployeeId{ Id = "222-22-2222",Name = "Spaulding Smails" },new EmployeeId{ Id = "333-33-3333",Name = "Ivan Ivanov" },new EmployeeId{ Id = "444-44-4444",Name = "Vasya Pupkin" }};// Build nationality collection.var empNationalities = new List<EmployeeNationality>() {new EmployeeNationality{ Id = "111-11-1111",Nationality = "American" },new EmployeeNationality{ Id = "333-33-3333",Nationality = "Russian" },new EmployeeNationality{ Id = "222-22-2222",Nationality = "Irish" },new EmployeeNationality{ Id = "444-44-4444",Nationality = "Russian" }};// Build query.var query = from emp in employeesjoin n in empNationalitieson emp.Id equals n.Idorderby n.Nationality descendingselect new {Id = emp.Id,Name = emp.Name,Nationality = n.Nationality};foreach( var person in query ) {Console.WriteLine( "{0}, {1}, \t{2}",person.Id,person.Name,person.Nationality );}}}


使用orderby语句

using System;using System.Linq;using System.Collections.Generic;public class Employee{public string LastName { get; set; }public string FirstName { get; set; }public string Nationality { get; set; }}public class OrderByExample{static void Main() {var employees = new List<Employee>() {new Employee {LastName = "Glasser", FirstName = "Ed",Nationality = "American"},new Employee {LastName = "Pupkin", FirstName = "Vasya",Nationality = "Russian"},new Employee {LastName = "Smails", FirstName = "Spaulding",Nationality = "Irish"},new Employee {LastName = "Ivanov", FirstName = "Ivan",Nationality = "Russian"}};var query = from emp in employeesorderby emp.Nationality,emp.LastName descending,emp.FirstName descendingselect emp;foreach( var item in query ) {Console.WriteLine( "{0},\t{1},\t{2}",item.LastName,item.FirstName,item.Nationality );}}}


在构造函数里对对象赋值

using System;using System.Linq;public class Result{public Result( int input, int output ) {Input = input;Output = output;}public int Input { get; set; }public int Output { get; set; }}public class Projector{static void Main() {int[] numbers = { 1, 2, 3, 4 };var query = from x in numbersselect new Result( x, x*2 );foreach( var item in query ) {Console.WriteLine( "Input = {0}, Output = {1}",item.Input,item.Output );}}}

let条件用法

using System;using System.Linq;using System.Collections.Generic;public class Employee{public string LastName { get; set; }public string FirstName { get; set; }}public class LetExample{static void Main() {var employees = new List<Employee>() {new Employee {LastName = "Glasser", FirstName = "Ed"},new Employee {LastName = "Pupkin", FirstName = "Vasya"},new Employee {LastName = "Smails", FirstName = "Spaulding"},new Employee {LastName = "Ivanov", FirstName = "Ivan"}};var query = from emp in employeeslet fullName = emp.FirstName +" " + emp.LastNameorderby fullNameselect fullName;foreach( var item in query ) {Console.WriteLine( item );}}}