LINQ的简单用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary2
{
public class Class1
{
public static void Main()
{
#region
创建一个数据源
//int[] arr = new int[8]{1,2,3,4,5,6,7,8};
新建LINQ查询
//var numQuery =
// from num in arr
// where num > 3
// select num;
执行查询
//foreach(int i in numQuery){
// Console.WriteLine("{0} ",i);
//}
// List<int> numbers1 = new List<int>() { 1, 2, 3, 4, 5 };
// List<int> numbers2 = new List<int>() { 12, 10, 13, 15, 11, 19, 16 };
// IEnumerable<int> topQuery =
// from numtop in numbers2
// where numtop % 2 == 0
// select numtop;
// IEnumerable<int> listQuery =
// from num in numbers1
// where num > 2
// orderby num descending
// select num;
// int count =
// (from num in numbers1
// where num > 2
// orderby num descending
// select num).Count();
// int count2 = listQuery.Count();
// foreach (int i in listQuery)
// {
// Console.WriteLine("{0} ", i);
// }
// Console.WriteLine("符合条件的一共有{0}和{1}条", count,count2);
// //调用平均数函数
// Double numAvg = NumAverage();
// Console.WriteLine("平均数是{0}",numAvg);
// Console.ReadLine();
//}
//public static Double NumAverage() {
// int[] arr = new int[5]{1,3,6,7,9};
// IEnumerable<int> numbers =
// from num in arr
// where num > 1
// select num;
// Double avg = numbers.Average();
// return avg;
//}
#endregion

#region 1.select的简单用法
List<Student> students = new List<Student>
{
new Student{Name="戈超",Score=100,Type="项目经理"},
new Student{Name="丁志强",Score=90,Type="开发人员"},
new Student{Name="张志伟",Score=99,Type="开发组长"},
new Student{Name="才才",Score=98,Type="开发人员"},
new Student{Name="王喆",Score=89,Type="开发人员"},
new Student{Name="付后成",Score=93,Type="开发人员"}
};
List<Student> students2 = new List<Student>
{
new Student{Name="付后成",Score=90},
new Student{Name="才才",Score=100}
};
var query = from student in students
select student.Name;
foreach (var name in query)
{
Console.WriteLine(name);
}
Console.WriteLine("============================");
#endregion

#region 2.select的匿名类型用法
var query2 = from student in students
select new
{
newname = "姓名是:" + student.Name
};
foreach (var student in query2)
{
Console.WriteLine(student.newname);
}
Console.WriteLine("============================");
#endregion

#region 3.select的条件用法
var query3 = from student in students
select new
{
student.Name,
level = student.Score > 95 ? "优秀" : "一般"
};
foreach (var student in query3)
{
Console.WriteLine("{0}:{1}", student.Name, student.level);
}
Console.WriteLine("============================");
#endregion

#region 4.select的筛选用法
var query4 = from student in students
where student.Name == "戈超"
select student;
foreach (var student in query4)
{
Console.WriteLine("姓名:{0},分数:{1}", student.Name, student.Score);
}
Console.WriteLine("============================");
#endregion

#region 5.select的嵌套类型用法
List<Student2> student2 = new List<Student2>
{
new Student2{Name="戈超",Score = new List<int>{60,70,80,90}},
new Student2{Name="戈庆",Score = new List<int>{60,70,80,90}},
new Student2{Name="王强",Score = new List<int>{65,75,85,95}},
new Student2{Name="付后成",Score = new List<int>{62,72,82,92}},
new Student2{Name="才才",Score = new List<int>{69,79,89,99}}
};
var query5 = from student in student2
select new
{
student.Name,
hightScore = from score in student.Score
select score
};
foreach (var student in query5)
{
Console.WriteLine("姓名:{0}", student.Name);
foreach (var sc in student.hightScore)
{
Console.WriteLine(sc);
}
}
Console.WriteLine("============================");
#endregion

#region 6.select的本地调用用法
var query6 = from student in students
select new
{
student.Name,
level = HightScore(student.Score)
};
foreach (var student in query6)
{
Console.WriteLine("{0}:{1}", student.Name, student.level);
}
Console.WriteLine("=====================");
#endregion

#region 7.select的Distinct用法
var query7 = (from student in students
select student.Name).Distinct();
foreach (var student in query7)
{
Console.WriteLine(student);
}
Console.WriteLine("============================");
#endregion

#region inner join关键字的用法
var query8 = from stu1 in students
join stu2 in students2 on stu1.Score equals stu2.Score
select stu1;
foreach (var stu in query8)
{
Console.WriteLine("姓名:{0} 分数:{1}", stu.Name, stu.Score);
}
Console.WriteLine("============================");
#endregion

#region group和into的用法
String[] words = { "black", "red", "blue", "green" };
var query9 = from word in words
//根据每个元素的首字母分组
group word by word[0] into colorGroup
where colorGroup.Count() >= 1
select new
{
firstlettle = colorGroup.Key,
Words = colorGroup.Count()
};

var query9_1 = from student in students
orderby student.Score descending
group student by student.Type into grpStudent
select new
{
grpStudent.Key,
num = grpStudent.Count(),
maxScore = grpStudent.Max(stu => stu.Score),
minScore = grpStudent.Min(stu => stu.Score),
numSum = grpStudent.Sum(stu => stu.Score),
};
foreach(var stu in query9_1){
Console.WriteLine("{0} {1} {2} {3}",stu.Key,stu.maxScore,stu.minScore,stu.numSum);
}
foreach (var w in query9)
{
Console.WriteLine("首字母:{0} 个数:{1}", w.firstlettle, w.Words);
}
Console.WriteLine("==================");
#endregion

#region let的用法
int[] arrList = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var query10 = from arr in arrList
let evenNumber = from a in arrList
where a % 2 == 0
select a
select evenNumber.Count() * arr;
foreach (var arr2 in query10)
{
Console.WriteLine(arr2);
}
Console.WriteLine("=======================");
#endregion

#region where的用法 1.常见的where语句
List<Student> student3 = new List<Student>
{
new Student{Name="chaoge",Score=100},
new Student{Name="dingzhiqiang",Score=90},
new Student{Name="zhangzhiwei",Score=99}
};
var query11 = from student in student3
where (student.Name.Length > 5 || student.Score % 2 == 0) && student.Name.Contains("zhi")
select student;
Console.WriteLine("满足条件的人为:");
foreach (var stu in query11)
{
Console.WriteLine("姓名:{0} 分数:{1}", stu.Name, stu.Score);
}
Console.WriteLine("=======================");
#endregion

#region 2.where使用自定义函数
var query12 = from student in student3
where HightScore2(student.Name)
select student;
foreach (var stu in query12)
{
Console.WriteLine("合格的人为:{0} 分数:{1}", stu.Name, stu.Score);
}
Console.WriteLine("=======================");
#endregion

#region 3.动词筛选
String[] wordList = { "chaoge", "chaogexxx" };
List<Student> student4 = new List<Student>
{
new Student{Name="chaoge",Score=100},
new Student{Name="chaogexxx",Score=90},
new Student{Name="dingzhiqiang",Score=90},
new Student{Name="zhangzhiwei",Score=99}
};
var query13 = from student in student4
where !wordList.Contains(student.Name)
select student;
Console.WriteLine("没有敏感词的是");
foreach (var stu in query13)
{
Console.WriteLine("姓名:{0} 分数:{1}", stu.Name, stu.Score);
}
Console.WriteLine("============================");
#endregion

#region 包含,类型SQL里的like %%
var query15 = student2.Where(stu2 => stu2.Name.Contains("戈")).Select(stu2 => stu2.Name);
foreach (var stu in query15)
{
Console.WriteLine(stu);
}
Console.WriteLine("============================");
#endregion

#region 泛型字典
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "戈超");
dic.Add(2, "丁志强");
dic.Add(3, "才才");
dic.Add(4, "付后成");
dic.Add(5, "王强");
//var query16 = from d in dic
// where d.Key % 2 == 0
// select d.Value;
var query16 = dic.Where(d => d.Key % 2 == 0).Select(d => d.Value);
foreach (var d in query16)
{
Console.WriteLine(d);
}
Console.WriteLine("============================");
#endregion

#region 对字符串的查询
String str = "My Name Is GeChao666";
var query17 = str.Where(s => Char.IsDigit(s)).Contains('6');
Console.WriteLine(query17);
Console.WriteLine("============================");
#endregion

#region 常用的简单计算
//求最大值
var query18 = (from student in students2
select student).Max(stu => stu.Score);
//求指定分数数目
var query19 = (from student in students
where student.Score > 95
select student).Count();
//求指定数目的总和
var query20 = (from student in students
where student.Score % 2 == 0
select student).Sum(stu => stu.Score);
Console.WriteLine("最大分数是:{0} 高于95分的有{1}个人 分数为偶数的分数总和:{2}", query18, query19, query20);
Console.WriteLine("============================");
#endregion

#region orderby desc/asc
var query21 = from student in students
orderby student.Score ascending
select student;
foreach(var stu in query21){
Console.WriteLine("姓名:{0} 分数:{1}",stu.Name,stu.Score);
}
Console.WriteLine("============================");
#endregion

#region 跳过前面多少条数据取余下的数据、取最后一个数据
var query22 = students.Where(stu => stu.Score > 90).Select(stu => stu.Name).Skip(1);
var query23 = students.Where(stu => stu.Score % 2 == 0).Select(stu => stu.Name).LastOrDefault();
foreach(var stu in query22){
Console.WriteLine(stu);
}
foreach (var stu in query23)
{
Console.WriteLine(stu);
}
Console.WriteLine("============================");
#endregion

#region 取一串数据的中间任意数目的数据
var query24 = students.OrderByDescending(stu => stu.Score).Select(stu => stu).Skip(1).Take(3).ToList();
foreach(var stu in query24){
Console.WriteLine("姓名:{0} 分数:{1}",stu.Name,stu.Score);
}
Console.WriteLine("============================");
#endregion

#region sql中的in
#endregion

Console.ReadLine();
}
public static String HightScore(int score)
{
if (score >= 95)
{
return "高分";
}
return "不合格";
}
public static Boolean HightScore2(String name)
{
if (name.Substring(1, 1) == "h")
{
return true;
}
return false;
}
public class Student
{
public String Name { get; set; }
public Int32 Score { get; set; }
public String Type { get; set; }
}
public class Student2
{
public String Name { get; set; }
public List<int> Score { get; set; }
}
}
}

转载于:https://www.cnblogs.com/GeChaoblog/p/5279756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值