LINQ 学习路程 -- 查询操作 Conversion Operators
2024-08-26 10:19:55
Method | Description |
---|---|
AsEnumerable | Returns the input sequence as IEnumerable<t> |
AsQueryable | Converts IEnumerable to IQueryable, to simulate a remote query provider |
Cast | Coverts a non-generic collection to a generic collection (IEnumerable to IEnumerable<T>) |
OfType | Filters a collection based on a specified type |
ToArray | Converts a collection to an array |
ToDictionary | Puts elements into a Dictionary based on key selector function |
ToList | Converts collection to List |
ToLookup | Groups elements into an Lookup<TKey,TElement> |
class Program
{ static void ReportTypeProperties<T>(T obj)
{
Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
Console.WriteLine("Actual type: {0}", obj.GetType().Name);
} static void Main(string[] args)
{
Student[] studentArray = {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = } ,
}; ReportTypeProperties( studentArray);
ReportTypeProperties(studentArray.AsEnumerable());
ReportTypeProperties(studentArray.AsQueryable());
}
} Cast
class Program
{
static void ReportTypeProperties<T>(T obj)
{
Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
Console.WriteLine("Actual type: {0}", obj.GetType().Name);
}
static void Main(string[] args)
{
Student[] studentArray = {
new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
};
ReportTypeProperties( studentArray);
ReportTypeProperties(studentArray.Cast<Student>());
}
}
IList<string> strList = new List<string>() {
"One",
"Two",
"Three",
"Four",
"Three"
};
string[] strArray = strList.ToArray<string>();// converts List to Array
IList<string> list = strArray.ToList<string>(); // converts array into list
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName = "John", age = 18 } ,
new Student() { StudentID = 2, StudentName = "Steve", age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , age = 20 } ,
new Student() { StudentID = 5, StudentName = "Ron" , age = 21 }
};
//following converts list into dictionary where StudentId is a key
IDictionary<int, Student> studentDict =
studentList.ToDictionary<Student, int>(s => s.StudentID);
foreach(var key in studentDict.Keys)
Console.WriteLine("Key: {0}, Value: {1}",
key, (studentDict[key] as Student).StudentName);
最新文章
- SQL优化技术分析-4:其他
- 增加SWAP空间的方法
- NativeScript 也能开发桌面应用 (nativescript-dotnet-runtime)
- js-JavaScript高级程序设计学习笔记16
- 基于WDF的PCI/PCIe接口卡Windows驱动程序(4)- 驱动程序代码(源文件)
- 《JAVA与模式》之享元模式
- Android拼图游戏
- C语言中的宏展开
- 小学生玩ACM----优先队列
- [AngularJS - app] AngularJS Location-picker app
- 根据引用jar包路径查找原JAR包
- echarts仪表盘如何设置图例(legend)
- CMake 条件判断
- 网络1712--c语言第二次作业总结
- bzoj 3704 昊昊的机油之GRST - 贪心
- gcc 无法编译c17程序解决办法
- 大话前端解析Json对象
- Spring IOC 相关的面试题
- s3c2140 开发板笔记
- SQL优化系列——查询优化器
热门文章
- Hibernate学习之单向多对一映射
- Atitit.获取approot&#160;api&#160;应用根路径&#160;java&#160;c#.net&#160;php&#160;asp
- bean对应mapper.xml字段
- JFinal中Controller的应用
- Swift中文教程(七)--协议,扩展和泛型
- Swing实现系统托盘
- android 各版本的区别
- ios - 使用@try、catch捕获异常:
- EhCache 集群 配置(RMI方式)
- 九度OJ 1199:找位置 (计数)