本文概述
- LINQ ToLookup运算符的语法
- 方法语法中的LINQ ToLookup()运算符示例
- 查询语法中的LINQ ToLookup()示例
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 EmployeeList<
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 Departmentforeach (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 Departmentforeach (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;
}}}
输出
文章图片
推荐阅读
- LINQ Cast()方法
- LINQ ToArray()方法
- LINQ ToList()方法
- LINQ skip操作符
- LINQ TakeWhile分区运算符
- LINQ take分区运算符
- LINQ分区运算符
- LINQ thenby降序运算符
- LINQ ThenBy运算符