En LINQ, la unión cruzada es un proceso en el que los elementos de dos secuencias se combinan entre sí, lo que significa que el elemento de la primera secuencia o colección se combina con los elementos de otra secuencia o colección sin ninguna selección de clave o condición de filtrado y el número de elementos presentes en la secuencia resultante es igual al producto de los elementos en las dos secuencias o colecciones fuente. O en otras palabras, podemos decir que la unión cruzada producirá un producto cartesiano. La unión cruzada también se conoce como unión completa. En la unión cruzada, no requerimos una palabra clave que especifique la condición para unirse .
Discutamos este concepto con la ayuda de ejemplos dados:
Ejemplo 1:
// C# program to illustrate the concept // of the cross join using System; using System.Linq; using System.Collections.Generic; // Employee details public class Employee { public int emp_id { get; set; } public string emp_name { get; set; } public string emp_lang { get; set; } public int dpt_id { get; set; } } // Employee department details public class Department { public int dpt_id { get; set; } public string emp_dept { get; set; } } class GFG { // Main method static public void Main() { List<Employee> emp = new List<Employee>() { new Employee() { emp_id = 101, emp_name = "Amit", emp_lang = "Java", dpt_id = 1 }, new Employee() { emp_id = 102, emp_name = "Mohit", emp_lang = "C#", dpt_id = 2 }, new Employee() { emp_id = 103, emp_name = "Sona", emp_lang = "Java", dpt_id = 1 }, new Employee() { emp_id = 104, emp_name = "Lana", emp_lang = "C++", dpt_id = 3 }, new Employee() { emp_id = 105, emp_name = "Roma", emp_lang = "C#", dpt_id = 2 }, }; List<Department> Dpt = new List<Department>() { new Department() { dpt_id = 1, emp_dept = "Designing" }, new Department() { dpt_id = 2, emp_dept = "Development" }, new Department() { dpt_id = 3, emp_dept = "JE" }, }; // Implementing Cross join using query syntax var res = from first in emp from second in Dpt select new { Employee_name = first.emp_name, Department_name = second.emp_dept }; // Display result foreach(var val in res) { Console.WriteLine(" Employee Name: {0} || Department Name: {1}", val.Employee_name, val.Department_name); } } }
Producción:
Employee Name: Amit || Department Name: Designing Employee Name: Amit || Department Name: Development Employee Name: Amit || Department Name: JE Employee Name: Mohit || Department Name: Designing Employee Name: Mohit || Department Name: Development Employee Name: Mohit || Department Name: JE Employee Name: Sona || Department Name: Designing Employee Name: Sona || Department Name: Development Employee Name: Sona || Department Name: JE Employee Name: Lana || Department Name: Designing Employee Name: Lana || Department Name: Development Employee Name: Lana || Department Name: JE Employee Name: Roma || Department Name: Designing Employee Name: Roma || Department Name: Development Employee Name: Roma || Department Name: JE
Ejemplo 2:
// C# program to illustrate the concept // of cross join using System; using System.Linq; using System.Collections.Generic; // Employee details public class Employee { public int emp_id { get; set; } public string emp_name { get; set; } public string emp_lang { get; set; } public int dpt_id { get; set; } } // Employee department details public class Department { public int dpt_id { get; set; } public string emp_dept { get; set; } } class GFG { // Main method static public void Main() { List<Employee> emp = new List<Employee>() { new Employee() { emp_id = 101, emp_name = "Amit", emp_lang = "Java", dpt_id = 1 }, new Employee() { emp_id = 102, emp_name = "Mohit", emp_lang = "C#", dpt_id = 2 }, }; List<Department> Dpt = new List<Department>() { new Department() { dpt_id = 1, emp_dept = "Designing" }, new Department() { dpt_id = 2, emp_dept = "Development" }, }; // Implementing Cross join using // SelectMany method and lambda // expression var res_1 = Dpt.SelectMany(e => emp,(x, y) => new { Department_name = x.emp_dept, Employee_name = y.emp_name}); // Display result foreach(var val in res_1) { Console.WriteLine(" Department Name: {0} || Employee Name: {1}", val.Department_name, val.Employee_name); } // Implementing Cross join using // Join method and lambda expression var res_2 = Dpt.Join(emp, x => true, y => true, (x, y) => new { Department_name = x.emp_dept, Employee_name = y.emp_name}); Console.WriteLine(); // Display result foreach(var val in res_2) { Console.WriteLine(" Department Name: {0} || Employee Name: {1}", val.Department_name, val.Employee_name); } } }
Producción:
Department Name: Designing || Employee Name: Amit Department Name: Designing || Employee Name: Mohit Department Name: Development || Employee Name: Amit Department Name: Development || Employee Name: Mohit Department Name: Designing || Employee Name: Amit Department Name: Designing || Employee Name: Mohit Department Name: Development || Employee Name: Amit Department Name: Development || Employee Name: Mohit
Publicación traducida automáticamente
Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA