使用 LINQ 连接多个数据源的 C# 程序
LINQ 被称为语言集成查询,它是在 .NET 3.5 中引入的。它为 .NET 语言提供了创建查询以从数据源检索数据的能力。在本文中,我们将讨论如何使用 LINQ 连接多个数据源。这里的数据源是指列表。所以我们使用列表集合创建三个包含学生详细信息的数据源,然后根据 id 连接数据,这在所有列表中都使用 join 关键字。
句法:
from iterator1 in data1
join iterator2 in data2
on iterator1.column_name equals iterator2.column_name
join iterator3 in data3
on iterator1.column_name equals iterator3.column_name
————————————————–
————————————————–
join iteratorn in datan
on iterator1.column_name equals iteratorn.column_name
其中 data 是数据源列表,迭代器用于从特定数据源获取数据
返回:它将根据比较的 column_names 返回匹配的行。
例子:
Input:
Student
new Student{id = 7058, name = "sravan kumar", dept_id = 1, add_id = 21},
new Student{id = 7059, name = "jyothika", dept_id = 2, add_id = 22},
new Student{id = 7072, name = "harsha", dept_id = 1, add_id = 22},
new Student{id = 7076, name = "khyathi", dept_id = 4, add_id = 27},
Department
new Department{dept_id = 1, dept_name = "CSE"},
new Department{dept_id = 2, dept_name = "CSE"},
new Department{dept_id = 3, dept_name = "IT"},
Address
new Address{add_id = 21, address_name = "hyd"},
new Address{add_id = 22, address_name = "railu-peta"},
new Address{add_id = 24, address_name = "chenchu-peta"},
Output:
ID: 7058--> Name: sravan kumar--> Department: CSE--> Address: hyd
ID: 7059--> Name: jyothika--> Department: CSE--> Address: railu-peta
ID: 7072--> Name: harsha--> Department: CSE--> Address: railu-peta
方法
1.通过声明变量,使用名为 Student、Department 和 Address 的列表创建三个数据源。
2.向这些列表添加值。
3.根据学生id、部门id、地址id进行join。
var result = (from stu in students
join dept in departments on stu.dept_id equals dept.dept_id
join add in addresses on stu.add_id equals add.add_id).ToList();
4.使用 select() 方法选择数据。
select new
{
ID = stu.id,
Name = stu.name,
DeptName = dept.dept_name,
address = add.address_name
}
5.显示每个循环的使用。
foreach (var e in result)
{
Console.WriteLine("\tID: " + e.ID + "--> Name: " +
e.Name + "--> Department: " +
e.DeptName + "--> Address: " + e.address);
}
例子:
C#
// C# program to join multiple data sources
// Using LINQ
using System;
using System.Linq;
using System.Collections.Generic;
// Variables for Student list
public class Student
{
public int id;
public string name;
public int dept_id;
public int add_id;
}
// Variables for Department list
public class Department
{
public int dept_id;
public string dept_name;
}
// Variables for Address list
public class Address
{
public int add_id;
public string address_name;
}
class GFG{
// Driver code
static void Main(string[] args)
{
// Enter data for Student list
List students = new List()
{
new Student{ id = 7058, name = "sravan kumar",
dept_id = 1, add_id = 21 },
new Student{ id = 7059, name = "jyothika",
dept_id = 2, add_id = 22 },
new Student{ id = 7072, name = "harsha",
dept_id = 1, add_id = 22 },
new Student{ id = 7076, name = "khyathi",
dept_id = 4, add_id = 27 },
};
List departments = new List()
{
new Department{ dept_id = 1, dept_name = "CSE" },
new Department{ dept_id = 2, dept_name = "CSE" },
new Department{ dept_id = 3, dept_name = "IT " },
};
List addresses = new List()
{
new Address{ add_id = 21, address_name = "hyd" },
new Address{ add_id = 22, address_name = "railu-peta" },
new Address{ add_id = 24, address_name = "chenchu-peta" },
};
// Join the students and other two tables
var result = (from stu in students
join dept in departments on stu
.dept_id equals dept
.dept_id
join add in addresses on stu
.add_id equals add.add_id
select new
{
ID = stu.id, Name = stu.name,
DeptName = dept.dept_name,
address = add.address_name
}).ToList();
// Display the result
foreach(var e in result)
{
Console.WriteLine("\tID: " + e.ID + "--> Name: " +
e.Name + "--> Department: " +
e.DeptName + "--> Address: " + e.address);
}
}
}
输出:
ID: 7058--> Name: sravan kumar--> Department: CSE--> Address: hyd
ID: 7059--> Name: jyothika--> Department: CSE--> Address: railu-peta
ID: 7072--> Name: harsha--> Department: CSE--> Address: railu-peta