C# LINQ使用案例参考-C/S开发框架

C#源码参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkConsole
{
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int>() { 1,2,3,4,5,6,7,8,9,10};
var numQuery = from num in numbers
where num % 2 == 0
select num;
foreach (var num in numQuery)
{
Console.WriteLine("{0,1}", num);
}
FormExpDemo2();
FormExpDemo();
FormExpDemo3();
WhereExpDemo();
SelectDemo();
GroupDemo();
IntoDemo();
ThenByDemo();
LetDemo();
JoinDemo();
Console.ReadLine();
}
public class CustomerInfo
{
public string Name { get; set; }
public int Age { get; set; }
public string Tel { get; set; }
public List<string> telTable { get; set; }
}
public static void FormExpDemo2()
{
List<CustomerInfo> customers = new List<CustomerInfo>
{
new CustomerInfo{ Name = "欧阳晓晓",Age = 35,Tel = "123"},
new CustomerInfo{ Name = "上官飘飘",Age = 17,Tel = "456"},
new CustomerInfo{ Name = "诸葛菲菲",Age = 23,Tel = "789"}
};
var query = from ci in customers
where ci.Age > 20
select ci;
foreach (var ci in query)
{
Console.WriteLine("姓名:{0}年龄:{1}电话:{2}", ci.Name, ci.Age, ci.Tel);
}
}
private static void FormExpDemo()
{
List<CustomerInfo> customers = new List<CustomerInfo>
{
new CustomerInfo { Name = "欧阳小小",Age= 35,telTable = new List<string> {"123","234"} },
new CustomerInfo { Name = "上官飘飘",Age= 35,telTable = new List<string> {"456","567"} },
new CustomerInfo { Name = "诸葛菲菲",Age= 35,telTable = new List<string> {"789","456"} },
};
var query = from ci in customers
from tel in ci.telTable
where tel.IndexOf("456") > -1
select ci;
foreach (var ci in query)
{
Console.WriteLine("姓名:{0}年龄:{1}", ci.Name, ci.Age);
foreach (var tel in ci.telTable)
{
Console.WriteLine(" 电话:{0}", tel);
}
}
}
private static void FormExpDemo3()
{
List<CustomerInfo> customers = new List<CustomerInfo>
{
new CustomerInfo{ Name = "欧阳晓晓",Age = 35,Tel = "123"},
new CustomerInfo{ Name = "上官飘飘",Age = 77,Tel = "456"},
new CustomerInfo{ Name = "诸葛菲菲",Age = 23,Tel = "789"}
};
List<CustomerInfo> customers2 = new List<CustomerInfo>
{
new CustomerInfo{ Name = "令狐冲",Age = 25,Tel = "123"},
new CustomerInfo{ Name = "东方不败",Age = 15,Tel = "456"},
new CustomerInfo{ Name = "任盈盈",Age = 13,Tel = "789"}
};
var query = from custo in customers
where custo.Age > 20
from custo2 in customers2
where custo2.Age < 30
select new { custo, custo2 };
foreach (var ci in query)
{
Console.WriteLine("{0},{1}", ci.custo.Name, ci.custo2.Name);
}
}
private static void WhereExpDemo()
{
List<CustomerInfo> clist = new List<CustomerInfo>
{
new CustomerInfo{ Name="欧阳晓晓", Age=35, Tel ="1330708****"},
new CustomerInfo{ Name="上官飘飘", Age=17, Tel ="1592842****"},
new CustomerInfo{ Name="令狐冲", Age=23, Tel ="1380524****"}
};
var query = from custo in clist
where (custo.Name.Length == 3 || custo.Name.Substring(0, 1) == "令") && custo.Age > 20
select custo;
foreach (var ci in query)
{
Console.WriteLine("姓名:{0}年龄:{1}电话:{2}", ci.Name, ci.Age, ci.Tel);
}
var query2 = from custo in clist
where (custo.Name.Length == 3 && ChechName(custo.Name))
select custo;
foreach (var ci in query2)
{
Console.WriteLine("姓名:{0}年龄:{1}电话:{2}", ci.Name, ci.Age, ci.Tel);
}
}
private static bool ChechName(string name)
{
if (name.Substring(0, 1) == "令")
return true;
else
return false;
}
private static void SelectDemo()
{
List<CustomerInfo> clist = new List<CustomerInfo>
{
new CustomerInfo{ Name="欧阳晓晓", Age=35, Tel ="1330708****"},
new CustomerInfo{ Name="上官飘飘", Age=17, Tel ="1592842****"},
new CustomerInfo{ Name="令狐冲", Age=23, Tel ="1380524****"}
};
string[] names = { "令狐冲", "任盈盈", "杨过", "小龙女", "欧阳小夏", "欧阳晓晓" };
var query = from custo in clist
where custo.Age < 30
select new MyCustomerInfo { Name = custo.Name, Tel = custo.Tel };
foreach (var ci in query)
{
Console.WriteLine("姓名:{0}电话:{1}类型{2}", ci.Name, ci.Tel, ci.GetType().FullName);
}
}
public class MyCustomerInfo
{
public string Name { get; set; }
public string Tel { get; set; }
}
static List<CustomerInfo> clist = new List<CustomerInfo>
{
new CustomerInfo{ Name="欧阳晓晓", Age=35, Tel ="1330708"},
new CustomerInfo{ Name="上官飘飘", Age=17, Tel ="1592842"},
new CustomerInfo{ Name="欧阳锦鹏", Age=35, Tel ="1330708"},
new CustomerInfo{ Name="上官无忌", Age=23, Tel ="1380524"}
};
private static void GroupDemo()
{
var query = from custo in clist
group custo by custo.Name.Substring(0, 2);
foreach (IGrouping<string, CustomerInfo> group in query)
{
Console.WriteLine("分组键:{0}", group.Key);
foreach (var ci in group)
{
Console.WriteLine("姓名:{0}电话:{1}", ci.Name, ci.Tel);
}
Console.WriteLine("*********************");
}
}
private static void IntoDemo()
{
var query = from custo in clist
group custo by custo.Name.Substring(0, 2) into gpcustomer
orderby gpcustomer.Key descending
select gpcustomer;
Console.WriteLine("into用于group子句");
foreach (var group in query)
{
Console.WriteLine("分组见:{0}", group.Key);
foreach (var ci in group)
{
Console.WriteLine("姓名:{0}电话:{1}", ci.Name, ci.Tel);
}
Console.WriteLine("***********************");
}
var query2 = from custo in clist
select new { NewName = custo.Name, NewAge = custo.Age } into newCustomer
orderby newCustomer.NewAge
select newCustomer;
Console.WriteLine("into用于select子句");
foreach (var ci in query2)
{
Console.WriteLine("{0}年龄:{1}", ci.NewName, ci.NewAge);
}
}
private static void ThenByDemo()
{
List<CustomerInfo> clist = new List<CustomerInfo>
{
new CustomerInfo{ Name="欧阳晓晓 ", Age=35, Tel ="1330708****"},
new CustomerInfo{ Name="上官飘飘 ", Age=17, Tel ="1592842****"},
new CustomerInfo{ Name="郭靖 ", Age=17, Tel ="1330708****"},
new CustomerInfo{ Name="黄蓉 ", Age=17, Tel ="1300524****"}
};
var query = from customer in clist
orderby customer.Age, customer.Name.Length
select customer;
Console.WriteLine("按年龄排列,按名字字数进行次要排序");
foreach (var ci in query)
{
Console.WriteLine("姓名:{0} 年龄:{1} 电话:{2}",ci.Name, ci.Age, ci.Tel);
}
var query2 = from customer in clist
orderby customer.Age descending , customer.Name.Length descending
select customer;
Console.WriteLine("\n按年龄排列,按名字字数进行降序次要排列");
foreach (var ci in query2)
{
Console.WriteLine("姓名:{0} 年龄:{1} 电话:{2}", ci.Name, ci.Age, ci.Tel);
}
}
private static void LetDemo()
{
var query = from custo in clist
let g = custo.Name.Substring(0, 1)
where g == "欧" || g == "上"
select custo;
foreach (var ci in query)
{
Console.WriteLine("姓名:{0} 年龄:{1} 电话:{2}", ci.Name, ci.Age, ci.Tel);
}
}
private static void JoinDemo()
{
List<CustomerInfo> clist = new List<CustomerInfo>
{
new CustomerInfo{ Name="欧阳晓晓", Age=35, Tel ="1330708****"},
new CustomerInfo{ Name="上官飘飘", Age=17, Tel ="1592842****"},
new CustomerInfo{ Name="郭靖", Age=17, Tel ="1330708****"},
new CustomerInfo{ Name="黄蓉", Age=17, Tel ="1300524****"}
};
List<CustomerTitle> titleList = new List<CustomerTitle>
{
new CustomerTitle{ Name="欧阳晓晓", Title="歌手"},
new CustomerTitle{ Name="郭靖", Title="大侠"},
new CustomerTitle{ Name="郭靖", Title="洪七公徒弟"},
new CustomerTitle{ Name="黄蓉", Title="才女"},
new CustomerTitle{ Name="黄蓉", Title="丐帮帮主"}
};
var query = from customer in clist
join title in titleList
on customer.Name equals title.Name
select new { Name = customer.Name, Age = customer.Age, Title = title.Title };
foreach (var ci in query)
{
Console.WriteLine("姓名:{0} 年龄:{1}{2}", ci.Name, ci.Age, ci.Title);
}
Console.WriteLine("\n根据姓名进行分组联结");
var query2 = from customer in clist
join title in titleList
on customer.Name equals title.Name into tgroup
select new { Name = customer.Name, Titles = tgroup };
foreach (var g in query2)
{
Console.WriteLine(g.Name);
foreach (var g2 in g.Titles)
{
Console.WriteLine(" {0}", g2.Title);
}
}
Console.WriteLine("\n左外部联结");
var query3 = from customer in clist
join title in titleList
on customer.Name equals title.Name into tgroup
from subTitle in tgroup.DefaultIfEmpty()
select new { Name = customer.Name, Title = (subTitle == null ? "空缺" : subTitle.Title) };
foreach (var ci in query3)
{
Console.WriteLine("姓名:{0} ", ci.Name, ci.Title);
}
}
public class CustomerTitle
{
public string Name { get; set; }
public string Title { get; set; }
}
}
}
