当前位置:网站首页>C# Linq Demo
C# Linq Demo
2022-07-05 23:18:00 【ou. cs】
select
Assign values to new types
List<Students> studentsSource = new List<Students>()
{
new Students()
{
Id= 1,
Name="11",
ClassId=1,
Age=25,
},
new Students()
{
Id= 2,
Name="22",
ClassId=2,
Age=22,
},
new Students()
{
Id= 3,
Name="33",
ClassId=1,
Age=33,
},
new Students()
{
Id= 4,
Name="44",
ClassId=2,
Age=21,
},
new Students()
{
Id= 5,
Name="55",
ClassId=5,
Age=55,
},
};
var query =studentsSource.Select(x => new {
id=x.Id,age=x.Age>50?999:0});
foreach (var item in query)
{
Console.WriteLine(item.id);
Console.WriteLine(item.age);
Console.WriteLine("\n");
}
join
List<StuClass> stuClasses = new List<StuClass>()
{
new StuClass{
Id=1,ClassName="11"},
new StuClass{
Id=2,ClassName="22"},
new StuClass{
Id=3,ClassName="33"},
new StuClass{
Id=4,ClassName="44"},
};
// == Change it into equals
{
var query2 = from s in studentsSource
join c in stuClasses on s.ClassId equals c.Id
select new StuAndClass {
Id=s.Id,
ClassId=s.ClassId,
Age=s.Age,
ClassName=c.ClassName,
};
var query3 = studentsSource.Join(stuClasses, s => s.ClassId, c => c.Id, (s, c) => new StuAndClass
{
Id = s.Id,
ClassId = s.ClassId,
Age = s.Age,
ClassName = c.ClassName,
});
}
other
// Get the first data
//var query3 = studentsSource.Take(2);
//var query4 = studentsSource.Skip(2);
//var query5 = studentsSource.OrderBy(s=>s.Name);
var query2 = studentsSource.Select(s => s.Name);
var query6 = studentsSource.Where(s=>s.Name.Contains("1"));
边栏推荐
- UVA11294-Wedding(2-SAT)
- LeetCode——Add Binary
- Realize reverse proxy client IP transparent transmission
- Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
- Getting started stm32--gpio (running lantern) (nanny level)
- (4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
- AsyncSocket长连接棒包装问题解决
- 【经典控制理论】自控实验总结
- Un article traite de la microstructure et des instructions de la classe
- Selenium+Pytest自动化测试框架实战
猜你喜欢
February 13, 2022-4-symmetric binary tree
The method and principle of viewing the last modification time of the web page
Hcip day 11 (BGP agreement)
CJ mccullem autograph: to dear Portland
利用LNMP实现wordpress站点搭建
Selenium+Pytest自动化测试框架实战
3:第一章:认识JVM规范2:JVM规范,简介;
[untitled]
数据库基础知识(面试)
Go language implementation principle -- map implementation principle
随机推荐
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
2.13 summary
Déterminer si un arbre binaire est un arbre binaire complet
Error when LabVIEW opens Ni instance finder
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
Common JVM tools and optimization strategies
视频标准二三事
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
Sum of two numbers, sum of three numbers (sort + double pointer)
golang代码检查工具
Negative sampling
一文搞定垃圾回收器
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Detailed explanation of pointer and array written test of C language
Alibaba Tianchi SQL training camp task4 learning notes
Development specification: interface unified return value format [resend]
两数之和、三数之和(排序+双指针)
asp.net弹出层实例
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)