Linq To Object 查询语句示例

本文深入探讨了LINQ查询的高级用法,包括使用Lambda表达式、枚举数字、数组对象赋值、Join语句、OrderBy语句及在构造函数里的对象赋值等。通过实例展示了如何高效地进行数据筛选、排序、操作与集成。
var query = from employee in employees
where employee.Salary > 100000
orderby employee.LastName, employee.FirstName
select 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 numbers
select 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 collection
var 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 employees
join n in empNationalities
on emp.Id equals n.Id
orderby n.Nationality descending
select 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 employees
orderby emp.Nationality,
emp.LastName descending,
emp.FirstName descending
select 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 numbers
select 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 employees
let fullName = emp.FirstName +
" " + emp.LastName
orderby fullName
select fullName;
foreach( var item in query ) {
Console.WriteLine( item );
}
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值