本文概述
LINQ中的ToLookup运算符是一种扩展方法, 用于从源中提取一组键/值对。在这里, 结果集合中的每个元素都是一个通用的Lookup对象。查找对象包含键和与键匹配的子序列项。
LINQ ToLookup运算符的语法
这是使用LINQ ToLookup运算符将集合作为键/值对返回的语法。
C#代码
var Emp = objEmployee.ToLookup(x => x.Department);
在以上语法中, 我们使用ToLookup运算符将” objEmployee”的集合转换为键/值对列表。
方法语法中的LINQ ToLookup()运算符示例
这是在方法语法中使用LINQ ToLookup()将输入集合项转换为键/值对列表的示例。
C#代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//create an objEmployee of the class Employee and create a list of Employee
List<Employee> objEmployee = new List<Employee>()
{
new Employee(){ Name="Akshay Tyagi", Department="IT", Country="India"}, new Employee(){ Name="Vaishali Tyagi", Department="Marketing", Country="Australia"}, new Employee(){ Name="Arpita Rai", Department="HR", Country="China"}, new Employee(){ Name="Shubham Ratogi", Department="Sales", Country="USA"}, new Employee(){ Name="Himanshu Tyagi", Department="Operations", Country="Canada"}
};
//objEmployee.ToLookup() method is used to print the value of the data in the pair/collection of items.
var Emp = objEmployee.ToLookup(x => x.Department);
Console.WriteLine("Grouping Employees by Department");
Console.WriteLine("---------------------------------");
foreach (var KeyValurPair in Emp)
{
Console.WriteLine(KeyValurPair.Key);
// Lookup employees by Department
foreach (var item in Emp[KeyValurPair.Key])
{
Console.WriteLine("\t" + item.Name + "\t" + item.Department + "\t" + item.Country);
}
}
Console.ReadLine();
}
}
class Employee
{
public string Name { get; set; }
public string Department { get; set; }
public string Country { get; set; }
}
}
在上面的示例中, 我们使用ToLookup方法将Employee按部门分组。由于ToLookup会生成键/值对, 因此我们在foreach循环中使用了它, 而内部循环则根据作为输入传递的Key提取值。
输出
查询语法中的LINQ ToLookup()示例
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Employee> objEmployee = new List<Employee>()
{
new Employee(){ Name="Ashish ", Department="Marketing", Country="India"}, new Employee(){ Name="John", Department="IT", Country="Australia"}, new Employee(){ Name="Kim", Department="Sales", Country="China"}, new Employee(){ Name="Marcia", Department="HR", Country="USA"}, new Employee(){ Name="John", Department="Operations", Country="Canada"}
};
var emp = (from employee in objEmployee select employee).ToLookup(x => x.Department);
Console.WriteLine("Grouping Employees by Department");
Console.WriteLine("---------------------------------");
foreach (var KeyValurPair in emp)
{
Console.WriteLine(KeyValurPair.Key);
// Lookup employees by Department
foreach (var item in emp[KeyValurPair.Key])
{
Console.WriteLine("\t" + item.Name + "\t" + item.Department + "\t" + item.Country);
}
}
Console.ReadLine();
}
}
class Employee
{
public string Name { get; set; }
public string Department { get; set; }
public string Country { get; set; }
}
}
输出