本文概述
- LINQ到列表或集合的语法
- LINQ到列表/集合的示例
LINQ到列表或集合的语法 这是在列表或集合上编写LINQ查询以获取所需元素的语法。
var result = from e in objEmpselect new{Name = e.Name, Location = e.Location};
在以上语法中, 我们编写了LINQ查询, 以从” objEmp” 集合/列表对象获取所需的数据。
LINQ到列表/集合的示例
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1{class Programme2{static void Main(string[] args){//create object objEmp of class Employee and create a list of the Employee informationList<
Employee>
objEmp = new List<
Employee>
(){new Employee { EmpId=1, Name = "Akshay", Location="Chennai" }, new Employee { EmpId=2, Name = "Vaishali", Location="Chennai" }, new Employee { EmpId=3, Name = "Priyanka", Location="Guntur" }, new Employee { EmpId=4, Name = "Preeti", Location ="Vizag"}, };
//here use the LINQ query to sort or select the element from the collection of datavar result = from e in objEmpwhere e.Location.Equals("Chennai")select new{Name = e.Name, Location = e.Location};
//foreach loop is used to print the value of the 'result' having the output of the LINQ queryforeach (var item in result){Console.WriteLine(item.Name + "\t | " + item.Location);
}Console.ReadLine();
}}//create class employeeclass Employee{public int EmpId { get;
set;
}public string Name { get;
set;
}public string Location { get;
set;
}}}
在上面的代码中, 我们在列表” objEmp” 上使用了LINQ查询, 以根据需求获取所需的元素。
【LINQ到列表/集合】输出
文章图片
推荐阅读
- LINQ to SQL选择查询
- LINQ转Int数组
- LINQ转字符串数组
- LINQ对象
- LINQ转字符串
- Android驱动学习-灯光系统总体框架
- Android Studio移动鼠标显示悬浮提示的设置方法
- ●UVA 10652 Board Wrapping
- android inline hook