using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoLast
{
class Program
{
static void Main(string[] args)
{
var array = new [] { 3, 5, 7,8, 9, 10,11,12,13,14,15,67, 34, 454 };
var array1 = new[] { 8, 9, 10, 11, 12, 13, 14};
var result = array.Where(a => a > 50).OrderByDescending(a=>a);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.WriteLine(result.Count());
Console.WriteLine(result.Max());
Console.WriteLine(result.Sum());
Console.WriteLine(result.Average());
var result1 = from m in array
where m % 2 == 1
orderby m ascending
select m;
foreach (var item in result1)
{
Console.WriteLine(item);
}
//var result2 = from m in array
// where m > 7
// where m < 16
// where m % 2 == 1
// orderby m ascending
// select m;
var result2 = from m in array
where m > 7 && m < 16 && m % 2 == 1
orderby m ascending
select m;
foreach (var item in result2)
{
Console.WriteLine(item);
}
var m4 = from m in array
from n in array1
where m == n
select new { var1 = m, var2 = n };
foreach (var item in m4)
{
Console.WriteLine(item);
}
ArrayList alist = new ArrayList();
alist.Add("say");
alist.Add(123);
alist.Add("hear");
alist.Add("look");
//指定过滤类型 ofType<数据类型>()
var childarr = from st in alist.OfType<int>() select st;
foreach (var item in childarr)
{
Console.WriteLine(item);
}
string[] names = { "Alono", "Zheng", "Yuan", "Song", "Simle", "Hsieh", "Small", "She", "Sza", "Sam", "Fa", "Iyl" };
//var queryResults = from n in names
// where n.StartsWith("S")
// orderby n descending//按照最后一个字母排序 ordeby n.Substring(n.Length - 1)
// select n;//查询语法
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S"));//方法语法 Lambda 表达式
// var queryResults = names.OrderByDescending(n => n).Where(n => n.StartsWith("S"));
foreach (var item in queryResults)
Console.WriteLine(item);
List<Customer> customers = new List<Customer> {
new Customer {ID ="A",City ="New York",Country ="USA",Region ="North America",Sales =9999},
new Customer {ID ="B",City ="New York",Country ="USA",Region ="North America",Sales =9999},
new Customer {ID ="C",City ="XiAn",Country ="China",Region ="Asia",Sales =7777},
new Customer {ID ="D",City ="New York",Country ="USA",Region ="North America",Sales =9999},
new Customer {ID ="E",City ="BeiJing",Country ="China",Region ="Asia",Sales =8888},
new Customer {ID ="F",City ="New York",Country ="USA",Region ="North America",Sales =9999}
};
//--------------------单值选择查询------------------------
var queryResults1 = customers.Select(c => c.Region).Distinct();
var queryResults2 = (from c in customers select c.Region).Distinct();
//------------------------Any和All---------------------------------
bool anyUSA = customers.Any(c => c.Country == "USA");
if (anyUSA)
Console.WriteLine("some customers are in USA");
else
Console.WriteLine("WAWA");
bool allAsia = customers.All(c => c.Region == "Asia");
if (allAsia)
Console.WriteLine("WAWA");
else
Console.WriteLine("All customers are in Asia");
//--------------------------------多级排序---------------------------------------
var queryReaults3 =
from n in customers
where n.Region == "Asia"
orderby n.Region, n.Country descending, n.City //查询语法 多级排序
select n;
var queryResults4 =
customers.OrderBy(c => c.Region).ThenByDescending(c => c.Country).ThenBy(c => c.City).Select(c => new { c.ID, c.Region, c.Country, c.City });
//方法语法 多级排序
//------------------------------------组合查询(group query)-------------------------------------
//组合查询中的数据通过一个键(Key)字段来组合,每一个组中的所有成员都共享这个字段值,在这个例子中 键字段是Region
//要计算每个组的总和,应先生成一个新的结果集cg
var queryResults5 = from c in customers
group c by c.Region into cg
select new { TotalSales = cg.Sum(c => c.Sales), Region = cg.Key };
var orderedResults = from cg in queryResults5
orderby cg.TotalSales descending
select cg;
foreach (var item in orderedResults)
Console.WriteLine(item.TotalSales + "\t:" + item.Region);
//------------------------Take 和 Skip----------------------------------
//Take() 从查询结果中提取前n个结果
//Skip() 从查询结果中跳过前n个结果 返回剩余的结果
foreach (var item in orderedResults.Take(2))
Console.WriteLine(item.TotalSales + "\t:" + item.Region);
//---------------------------First 和 FirstOrDefault-------------------------------
//First() 返回结果集中第一个匹配给定条件的元素
//FirstOrDefault() 当查询条件不满足是,将为列表返回默认元素 而使用First()则返回null
Console.WriteLine(queryReaults3.FirstOrDefault(n => n.Region == "Asia"));
//--------------------------------集运算符-----------------------------
List<Order> orders = new List<Order>{
new Order {ID="A",Amount=100},
new Order {ID ="B",Amount =200},
new Order {ID ="H",Amount =300}};
var customersIDs = from c in customers
select c.ID;
var ordersIDs = from o in orders
select o.ID;
var customersWithOrders = customersIDs.Intersect(ordersIDs);//Intersect()
foreach (var item in customersWithOrders)
Console.WriteLine(item);
Console.WriteLine("-------------------------");
var ordersNoCustomers = ordersIDs.Except(customersIDs);//Except()
foreach (var item in ordersNoCustomers)
Console.WriteLine(item);
Console.WriteLine("-------------------------");
var allCustomersOrders = ordersIDs.Union(customersIDs);//Union()
foreach (var item in allCustomersOrders)
Console.WriteLine(item);
//Console.ReadKey();
//---------------------------------Join----------------------------
//使用Join运算符在一个查询中查找多个集合中的相关数据,用键字段把结果连接起来
var queryResults9 =
from c in customers
join o in orders on c.ID equals o.ID
select new { c.ID, c.City, SalesBefore = c.Sales, NewOrder = o.Amount, SalesAfter = c.Sales + o.Amount };
foreach (var item in queryResults9)
Console.WriteLine(item);
TestClass.Test();
Console.ReadLine();
}
class Order
{
public string ID { get; set; }
public decimal Amount { get; set; }
}
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()//重写ToString(),默认的ToString()仅输出类型名称
{
return "ID:" + ID + "City:" + City + "Country:" + Country + "Region:" + Region + "Sales:" + Sales;
}
}
}
public static class TestClass
{
public static void Test()
{
//about foreach and delegate, action etc
List<int> arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
arr.ForEach(new Action<int>(delegate (int a) { Console.WriteLine(a); }));
arr.ForEach(new Action<int>(a => Console.WriteLine(a)));
arr.ForEach(a => Console.WriteLine(a));
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoLast
{
class Program
{
static void Main(string[] args)
{
var array = new [] { 3, 5, 7,8, 9, 10,11,12,13,14,15,67, 34, 454 };
var array1 = new[] { 8, 9, 10, 11, 12, 13, 14};
var result = array.Where(a => a > 50).OrderByDescending(a=>a);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.WriteLine(result.Count());
Console.WriteLine(result.Max());
Console.WriteLine(result.Sum());
Console.WriteLine(result.Average());
var result1 = from m in array
where m % 2 == 1
orderby m ascending
select m;
foreach (var item in result1)
{
Console.WriteLine(item);
}
//var result2 = from m in array
// where m > 7
// where m < 16
// where m % 2 == 1
// orderby m ascending
// select m;
var result2 = from m in array
where m > 7 && m < 16 && m % 2 == 1
orderby m ascending
select m;
foreach (var item in result2)
{
Console.WriteLine(item);
}
var m4 = from m in array
from n in array1
where m == n
select new { var1 = m, var2 = n };
foreach (var item in m4)
{
Console.WriteLine(item);
}
ArrayList alist = new ArrayList();
alist.Add("say");
alist.Add(123);
alist.Add("hear");
alist.Add("look");
//指定过滤类型 ofType<数据类型>()
var childarr = from st in alist.OfType<int>() select st;
foreach (var item in childarr)
{
Console.WriteLine(item);
}
string[] names = { "Alono", "Zheng", "Yuan", "Song", "Simle", "Hsieh", "Small", "She", "Sza", "Sam", "Fa", "Iyl" };
//var queryResults = from n in names
// where n.StartsWith("S")
// orderby n descending//按照最后一个字母排序 ordeby n.Substring(n.Length - 1)
// select n;//查询语法
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S"));//方法语法 Lambda 表达式
// var queryResults = names.OrderByDescending(n => n).Where(n => n.StartsWith("S"));
foreach (var item in queryResults)
Console.WriteLine(item);
List<Customer> customers = new List<Customer> {
new Customer {ID ="A",City ="New York",Country ="USA",Region ="North America",Sales =9999},
new Customer {ID ="B",City ="New York",Country ="USA",Region ="North America",Sales =9999},
new Customer {ID ="C",City ="XiAn",Country ="China",Region ="Asia",Sales =7777},
new Customer {ID ="D",City ="New York",Country ="USA",Region ="North America",Sales =9999},
new Customer {ID ="E",City ="BeiJing",Country ="China",Region ="Asia",Sales =8888},
new Customer {ID ="F",City ="New York",Country ="USA",Region ="North America",Sales =9999}
};
//--------------------单值选择查询------------------------
var queryResults1 = customers.Select(c => c.Region).Distinct();
var queryResults2 = (from c in customers select c.Region).Distinct();
//------------------------Any和All---------------------------------
bool anyUSA = customers.Any(c => c.Country == "USA");
if (anyUSA)
Console.WriteLine("some customers are in USA");
else
Console.WriteLine("WAWA");
bool allAsia = customers.All(c => c.Region == "Asia");
if (allAsia)
Console.WriteLine("WAWA");
else
Console.WriteLine("All customers are in Asia");
//--------------------------------多级排序---------------------------------------
var queryReaults3 =
from n in customers
where n.Region == "Asia"
orderby n.Region, n.Country descending, n.City //查询语法 多级排序
select n;
var queryResults4 =
customers.OrderBy(c => c.Region).ThenByDescending(c => c.Country).ThenBy(c => c.City).Select(c => new { c.ID, c.Region, c.Country, c.City });
//方法语法 多级排序
//------------------------------------组合查询(group query)-------------------------------------
//组合查询中的数据通过一个键(Key)字段来组合,每一个组中的所有成员都共享这个字段值,在这个例子中 键字段是Region
//要计算每个组的总和,应先生成一个新的结果集cg
var queryResults5 = from c in customers
group c by c.Region into cg
select new { TotalSales = cg.Sum(c => c.Sales), Region = cg.Key };
var orderedResults = from cg in queryResults5
orderby cg.TotalSales descending
select cg;
foreach (var item in orderedResults)
Console.WriteLine(item.TotalSales + "\t:" + item.Region);
//------------------------Take 和 Skip----------------------------------
//Take() 从查询结果中提取前n个结果
//Skip() 从查询结果中跳过前n个结果 返回剩余的结果
foreach (var item in orderedResults.Take(2))
Console.WriteLine(item.TotalSales + "\t:" + item.Region);
//---------------------------First 和 FirstOrDefault-------------------------------
//First() 返回结果集中第一个匹配给定条件的元素
//FirstOrDefault() 当查询条件不满足是,将为列表返回默认元素 而使用First()则返回null
Console.WriteLine(queryReaults3.FirstOrDefault(n => n.Region == "Asia"));
//--------------------------------集运算符-----------------------------
List<Order> orders = new List<Order>{
new Order {ID="A",Amount=100},
new Order {ID ="B",Amount =200},
new Order {ID ="H",Amount =300}};
var customersIDs = from c in customers
select c.ID;
var ordersIDs = from o in orders
select o.ID;
var customersWithOrders = customersIDs.Intersect(ordersIDs);//Intersect()
foreach (var item in customersWithOrders)
Console.WriteLine(item);
Console.WriteLine("-------------------------");
var ordersNoCustomers = ordersIDs.Except(customersIDs);//Except()
foreach (var item in ordersNoCustomers)
Console.WriteLine(item);
Console.WriteLine("-------------------------");
var allCustomersOrders = ordersIDs.Union(customersIDs);//Union()
foreach (var item in allCustomersOrders)
Console.WriteLine(item);
//Console.ReadKey();
//---------------------------------Join----------------------------
//使用Join运算符在一个查询中查找多个集合中的相关数据,用键字段把结果连接起来
var queryResults9 =
from c in customers
join o in orders on c.ID equals o.ID
select new { c.ID, c.City, SalesBefore = c.Sales, NewOrder = o.Amount, SalesAfter = c.Sales + o.Amount };
foreach (var item in queryResults9)
Console.WriteLine(item);
TestClass.Test();
Console.ReadLine();
}
class Order
{
public string ID { get; set; }
public decimal Amount { get; set; }
}
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()//重写ToString(),默认的ToString()仅输出类型名称
{
return "ID:" + ID + "City:" + City + "Country:" + Country + "Region:" + Region + "Sales:" + Sales;
}
}
}
public static class TestClass
{
public static void Test()
{
//about foreach and delegate, action etc
List<int> arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
arr.ForEach(new Action<int>(delegate (int a) { Console.WriteLine(a); }));
arr.ForEach(new Action<int>(a => Console.WriteLine(a)));
arr.ForEach(a => Console.WriteLine(a));
}
}
}