using System;
using System.Linq;
using System.Collections.Generic;
using LinQ;
namespace LinQ
{
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
}
#region Linq语句的语法
private static void BasicConcept()
{
int[] numbers = { 5, 10, 8, 3, 6, 12 };
//Linq 语句的两种语法实现
//1.Query syntax
var numQuery1 = from num in numbers
where num % 2 == 0
orderby num
select num;
//2.Method syntax
var numQuery2 = numbers.Where(n => n % 2 == 0).OrderBy(n => n);
foreach (var i in numQuery1)
{
Console.WriteLine(i + "");
}
Console.WriteLine();
foreach (var i in numQuery2)
{
Console.WriteLine(i + "");
}
}
#endregion
#region query 的基本组成 Expamle
private static void QuerySyntax()
{
//1.data source
//必须实现IEnumerble接口
int[] numbers = { 0, 1, 2, 3, 4, 5, 6 };
//2.Query creation
var numquery = from nums in numbers
where (nums % 2) == 0
select nums;
//query语句强制执行
//int temp = numquery.Count();
List<int> list = numquery.ToList<int>();
numquery.ToArray<int>();
//3.Query execution
foreach (var num in numquery)
{
Console.WriteLine("{0,1}", num);
}
}
#endregion
#region Query语句的基本操作
private static void QueyOperation()
{
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7 };
var query = from num in numbers
where num % 2 == 1 || num % 3 == 1
orderby num descending
select num;
foreach (var num in query)
{
Console.WriteLine("{0}",num);
}
List<Customer> customer = new List<Customer>();
customer.Add(new Customer() { Name = "Jack", City = "Beijing" });
customer.Add(new Customer() { Name = "Lilei", City = "Beijing" });
customer.Add(new Customer() { Name = "Lili", City = "Shanghai" });
List<Employee> employees = new List<Employee>();
employees.Add(new Employee(){Name="Jack",ID=101});
employees.Add(new Employee() { Name = "Emily", ID = 102 });
var QueryCustomers = from c in customer
group c by c.City;
foreach (var cg in QueryCustomers)
{
Console.WriteLine(cg.Key);
foreach (var c in cg)
{
Console.WriteLine("{0}", c.Name);
}
}
var queryjoin = from c in customer
join e in employees on c.Name equals e.Name
select new { PersonName = c.Name, PersonID = e.ID, Persioncity = c.City };
foreach (var p in queryjoin)
{
Console.WriteLine("{0},{1},{2}", p.PersonName, p.PersonID, p.Persioncity);
}
//Linq的into的使用
var QueryCustomers1 = from c in customer
group c by c.City into cusGroup
where cusGroup.Count() >= 2
select new { City = cusGroup.Key, Number = cusGroup.Count() };
foreach (var c in QueryCustomers1)
{
Console.WriteLine("{0} count {1}", c.City, c.Number);
}
//let主要处理中间变量
string[] strings = { "hello hello", "world world", "happy happy" };
var stringQuery = from s in strings
let words = s.Split(' ')
from word in words
let w = word.ToUpper()
select w;
}
#endregion
}
class Customer
{
public string Name
{
get;
set;
}
public string City
{
get;
set;
}
}
class Employee
{
public string Name { get; set; }
public int ID { get; set; }
}
}
using System.Linq;
using System.Collections.Generic;
using LinQ;
namespace LinQ
{
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
}
#region Linq语句的语法
private static void BasicConcept()
{
int[] numbers = { 5, 10, 8, 3, 6, 12 };
//Linq 语句的两种语法实现
//1.Query syntax
var numQuery1 = from num in numbers
where num % 2 == 0
orderby num
select num;
//2.Method syntax
var numQuery2 = numbers.Where(n => n % 2 == 0).OrderBy(n => n);
foreach (var i in numQuery1)
{
Console.WriteLine(i + "");
}
Console.WriteLine();
foreach (var i in numQuery2)
{
Console.WriteLine(i + "");
}
}
#endregion
#region query 的基本组成 Expamle
private static void QuerySyntax()
{
//1.data source
//必须实现IEnumerble接口
int[] numbers = { 0, 1, 2, 3, 4, 5, 6 };
//2.Query creation
var numquery = from nums in numbers
where (nums % 2) == 0
select nums;
//query语句强制执行
//int temp = numquery.Count();
List<int> list = numquery.ToList<int>();
numquery.ToArray<int>();
//3.Query execution
foreach (var num in numquery)
{
Console.WriteLine("{0,1}", num);
}
}
#endregion
#region Query语句的基本操作
private static void QueyOperation()
{
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7 };
var query = from num in numbers
where num % 2 == 1 || num % 3 == 1
orderby num descending
select num;
foreach (var num in query)
{
Console.WriteLine("{0}",num);
}
List<Customer> customer = new List<Customer>();
customer.Add(new Customer() { Name = "Jack", City = "Beijing" });
customer.Add(new Customer() { Name = "Lilei", City = "Beijing" });
customer.Add(new Customer() { Name = "Lili", City = "Shanghai" });
List<Employee> employees = new List<Employee>();
employees.Add(new Employee(){Name="Jack",ID=101});
employees.Add(new Employee() { Name = "Emily", ID = 102 });
var QueryCustomers = from c in customer
group c by c.City;
foreach (var cg in QueryCustomers)
{
Console.WriteLine(cg.Key);
foreach (var c in cg)
{
Console.WriteLine("{0}", c.Name);
}
}
var queryjoin = from c in customer
join e in employees on c.Name equals e.Name
select new { PersonName = c.Name, PersonID = e.ID, Persioncity = c.City };
foreach (var p in queryjoin)
{
Console.WriteLine("{0},{1},{2}", p.PersonName, p.PersonID, p.Persioncity);
}
//Linq的into的使用
var QueryCustomers1 = from c in customer
group c by c.City into cusGroup
where cusGroup.Count() >= 2
select new { City = cusGroup.Key, Number = cusGroup.Count() };
foreach (var c in QueryCustomers1)
{
Console.WriteLine("{0} count {1}", c.City, c.Number);
}
//let主要处理中间变量
string[] strings = { "hello hello", "world world", "happy happy" };
var stringQuery = from s in strings
let words = s.Split(' ')
from word in words
let w = word.ToUpper()
select w;
}
#endregion
}
class Customer
{
public string Name
{
get;
set;
}
public string City
{
get;
set;
}
}
class Employee
{
public string Name { get; set; }
public int ID { get; set; }
}
}