当前位置:网站首页>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"));
边栏推荐
- UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
- 两数之和、三数之和(排序+双指针)
- 无刷驱动设计——浅谈MOS驱动电路
- What is the process of building a website
- (4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
- 一文搞定class的微觀結構和指令
- Practice of concurrent search
- openresty ngx_lua正则表达式
- grafana工具界面显示报错influxDB Error
- MySQL (2) -- simple query, conditional query
猜你喜欢

终于搞懂什么是动态规划的

Three.JS VR看房

Marginal probability and conditional probability

Hcip day 12 (BGP black hole, anti ring, configuration)

Debian 10 installation configuration

Selenium+Pytest自动化测试框架实战

Element operation and element waiting in Web Automation

Vision Transformer (ViT)

【经典控制理论】自控实验总结

SPSS analysis of employment problems of college graduates
随机推荐
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
媒体查询:引入资源
一文搞定class的微觀結構和指令
Use of shell:for loop
Use of grpc interceptor
JVM的简介
openresty ngx_lua正则表达式
openresty ngx_lua请求响应
Selenium+Pytest自动化测试框架实战
Krypton Factor-紫书第七章暴力求解
如何快速理解复杂业务,系统思考问题?
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
golang代码检查工具
基于脉冲神经网络的物体检测
二叉树递归套路总结
Week 17 homework
查看网页最后修改时间方法以及原理简介
Media query: importing resources
February 13, 2022-4-symmetric binary tree