linq的基本使用
2024-08-25 21:48:32
1.查询所有的偶数
int[] str = { , , , , , , , , , , , , };
var elist = from s in str
where s % ==
select s;
foreach (int i in elist)
{
Console.WriteLine("偶数有:{0}", i);
}
Console.ReadKey();
2.查询所有的偶数 从大到小排序
int[] str = { , , , , , , , , , , , , };
var elist = from s in str
where s % ==
orderby s descending ////查询所有偶数并倒序排序,就是从大到小排序。
select s;
foreach (int i in elist)
{
Console.WriteLine("偶数有:{0}", i);
}
3.查询对象结果投影一个新的对象
class Student
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }
} List<Student> list = new List<Student>()
{
new Student(){name=@"haha",sex="女",age=},
new Student(){name=@"hehe",sex="女",age=},
new Student(){name=@"heihei",sex="男",age=}
};
//var stu = from s in list
// where s.age > 10
// select s;
//foreach (var bb in stu)
//{
// Console.WriteLine("{0},{1},{2}", bb.name, bb.age, bb.sex);
//}
//将查询结果直接放入一个新的对象
var stu = from s in list
where s.age >
select new { Newname = s.name, Newsex = s.sex, Newage = s.age };
foreach (var bb in stu)
{
Console.WriteLine("{0},{1},{2}", bb.Newname, bb.Newage, bb.Newsex);
}
4.获取集合对象的,长度大于7对象。
string[] arr = { "qwewrtyywe", "asdfghkl", "zxcvbnm", "abcqqqqqq" };
IEnumerable<string> str = from s in arr
where s.Length >
select s;
foreach (var ab in str)
{
Console.WriteLine(ab);
}
最新文章
- iOS开发之手势识别
- Windows平台下Git服务器搭建
- 内存对齐 和 sizeof小结
- 7、SQL Server索引、表压缩
- APICloud支持Atom编辑器,并建立开发工具核心库
- html dom的加载
- 15 Linux Split and Join Command Examples to Manage Large Files--reference
- 【BZOJ3439】 Kpm的MC密码 (TRIE+主席树)
- Google Web Toolkit (GWT)怎么制作多个用户界面
- 单服务器防护linux iptables脚本
- 目前比较流行的Python科学计算发行版
- thinkphp 实现微信公众号开发(一)
- 转:WebDriver进行屏幕截图
- spring调用mongodb
- 2017 CCPC 哈尔滨站 HDU 6242
- 前端学习历程--http与https
- jupyter notebook的安装与基本操作
- linux source code search
- 解决无法安装Microsoft .Net Framework 3.5
- React 懒加载组件
热门文章
- Node + Redis 实现分布式Session方案(转载)
- P2P原理(转)
- MySQL插入去重命令_REPLACE INTO
- c#高级编程第七版 学习笔记 第二章 核心c#
- [C++ Primer Plus] 第3章、处理数据(二)课后习题
- vue搭建前端相关命令
- 05. .stop、.prevent、.capture、.self、.once、
- 【转载】Selenium WebDriver的简单操作说明
- 20190316xlVba_设置行高的改进方案
- FileProvider 添加二级目录