如何为多个DataTable创建查询语法以实现Sql Server的IN运算符

发布于 2024-09-02 21:38:55 字数 6448 浏览 4 评论 0原文

我通过执行存储过程获取了 3-4 个表。现在它们驻留在我的数据集中。

我必须为多种表单维护此数据集,并且我不会对此数据集执行任何 DML 操作。

现在这个数据集包含 4 个表,我必须从中获取一些记录来显示数据。 表中存储的数据以一对多关系的形式存在。

即在交易的情况下。每条记录有 N 条记录。然后这N条记录进一步映射到第3个表的M条记录。

表 1

MAP_ID               GUEST_ID             DEPARTMENT_ID        PARENT_ID            PREFERENCE_ID
-------------------- -------------------- -------------------- -------------------- --------------------
19                   61                   1                    1                    5
14                   61                   1                    5                    15
15                   61                   2                    4                    10
18                   61                   2                    13                   23
17                   61                   2                    20                   26
16                   61                   40                   40                   41
20                   62                   1                    5                    14
21                   62                   1                    5                    15
22                   62                   1                    6                    16
24                   62                   2                    3                    4
23                   62                   2                    4                    9
27                   62                   2                    13                   23
25                   62                   2                    20                   24
26                   62                   2                    20                   25
28                   63                   1                    1                    5
29                   63                   1                    1                    8
34                   63                   1                    5                    15
30                   63                   2                    4                    10
33                   63                   2                    4                    11
31                   63                   2                    13                   23
32                   63                   40                   40                   41
35                   65                   1                    NULL                 1
36                   65                   1                    NULL                 1
38                   68                   2                    13                   22
37                   68                   2                    20                   25
39                   68                   2                    23                   27
40                   92                   1                    NULL                 1

表 2

Department_ID        Department_Name         Parent_Id             Parent_Name                                                                                                                                                                                                                                              
-------------------- ----------------------- ---------------   ----------------------------------------------------------------------------------
1                    Food                    1, 5, 6               Food, North Indian, South Indian                                                                                                                                                                                                                                           
2                    Lodging                 3, 4, 13, 20, 23      Room, Floor, Non Air Conditioned, With Balcony, Without Balcony                                                                                                                                                                                                                                               
40                   New                     40                    SubNew

表 3

Parent_Id            Parent_Name                                        Preference_ID             Preference_Name
-------------------- -----------------------------------------------  -----------------------  -------------------
NULL                 NULL                                               NULL                     NULL
1                    Food                                               5, 8                     North Indian, Italian
3                    Room                                               4                        Floor
4                    Floor                                              9, 10, 11                First, Second, Third
5                    North Indian                                       14, 15                   X, Y
6                    South Indian                                       16                       Dosa
13                   Non Air Conditioned                                22, 23                   With Balcony, Without Balcony
20                   With Balcony                                       24, 25, 26               Mountain View, Ocean View, Garden View
23                   Without Balcony                                    27                       Mountain View
40                   New                                                41           SubNew 

我有这 3 个表,它们以某种方式相关。

表 1 将是这 2 个表(即表 2 和表 3)的主表。

我需要对它们进行查询,因为

        SELECT Department_Id, Department_Name, Parent_Name FROM Table2 WHERE Department_Id in 
            (
                SELECT Department_Id FROM Table1 WHERE guest_id=65
            )


        SELECT Parent_Id, Parent_Name, Preference_Name FROM Table3 WHERE PARENT_ID in 
            (
                SELECT parent_id FROM Table1 WHERE guest_id=65 
            )

现在我需要在 DataTables 上使用这些查询。

所以我为此使用查询语法并达到了这一点。

        var dept_list=  from dept in DtMapGuestDepartment.AsEnumerable()
                        where dept.Field<long>("PK_GUEST_ID")==long.Parse(63)
                        select dept;

这应该为我提供 guest id = 63 的所有部门的列表。

现在我想从表 2 中选择所有 Departments_name 和 Parent_name,其中 guest_id=63 即我在上面获取的部门。

表 3 也将遵循同样的情况。

请建议如何执行此操作。

感谢您耐心阅读我的问题。

I have fetched 3-4 tables by executing my stored procedure. Now they resides on my dataset.

I have to maintain this dataset for multiple forms and I am not doing any DML operation on this dataset.

Now this dataset contains 4 tables out of which i have to fetch some records to display data.
Data stored in tables are in form of one to many relationship.

i.e. In case of transactions. N records per record. Then these N records are further mapped to M records of 3rd table.

Table 1

MAP_ID               GUEST_ID             DEPARTMENT_ID        PARENT_ID            PREFERENCE_ID
-------------------- -------------------- -------------------- -------------------- --------------------
19                   61                   1                    1                    5
14                   61                   1                    5                    15
15                   61                   2                    4                    10
18                   61                   2                    13                   23
17                   61                   2                    20                   26
16                   61                   40                   40                   41
20                   62                   1                    5                    14
21                   62                   1                    5                    15
22                   62                   1                    6                    16
24                   62                   2                    3                    4
23                   62                   2                    4                    9
27                   62                   2                    13                   23
25                   62                   2                    20                   24
26                   62                   2                    20                   25
28                   63                   1                    1                    5
29                   63                   1                    1                    8
34                   63                   1                    5                    15
30                   63                   2                    4                    10
33                   63                   2                    4                    11
31                   63                   2                    13                   23
32                   63                   40                   40                   41
35                   65                   1                    NULL                 1
36                   65                   1                    NULL                 1
38                   68                   2                    13                   22
37                   68                   2                    20                   25
39                   68                   2                    23                   27
40                   92                   1                    NULL                 1

Table 2

Department_ID        Department_Name         Parent_Id             Parent_Name                                                                                                                                                                                                                                              
-------------------- ----------------------- ---------------   ----------------------------------------------------------------------------------
1                    Food                    1, 5, 6               Food, North Indian, South Indian                                                                                                                                                                                                                                           
2                    Lodging                 3, 4, 13, 20, 23      Room, Floor, Non Air Conditioned, With Balcony, Without Balcony                                                                                                                                                                                                                                               
40                   New                     40                    SubNew

TABLE 3

Parent_Id            Parent_Name                                        Preference_ID             Preference_Name
-------------------- -----------------------------------------------  -----------------------  -------------------
NULL                 NULL                                               NULL                     NULL
1                    Food                                               5, 8                     North Indian, Italian
3                    Room                                               4                        Floor
4                    Floor                                              9, 10, 11                First, Second, Third
5                    North Indian                                       14, 15                   X, Y
6                    South Indian                                       16                       Dosa
13                   Non Air Conditioned                                22, 23                   With Balcony, Without Balcony
20                   With Balcony                                       24, 25, 26               Mountain View, Ocean View, Garden View
23                   Without Balcony                                    27                       Mountain View
40                   New                                                41           SubNew 

I have these 3 tables that are related in some fashion like this.

Table 1 will be the master for these 2 tables i.e. table 2 and table 3.

I need to query on them as

        SELECT Department_Id, Department_Name, Parent_Name FROM Table2 WHERE Department_Id in 
            (
                SELECT Department_Id FROM Table1 WHERE guest_id=65
            )


        SELECT Parent_Id, Parent_Name, Preference_Name FROM Table3 WHERE PARENT_ID in 
            (
                SELECT parent_id FROM Table1 WHERE guest_id=65 
            )

Now I need to use these queries on DataTables.

So I am using Query Syntax for this and reached up to this point.

        var dept_list=  from dept in DtMapGuestDepartment.AsEnumerable()
                        where dept.Field<long>("PK_GUEST_ID")==long.Parse(63)
                        select dept;

This should give me the list of all departments that has guest id =63

Now I want to select all departments_name and parent_name from Table 2 where guest_id=63 i.e. departments that i fetched above.

This same case will be followed for Table3.

Please suggest how to do this.

Thanks for keeping up patience for reading my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-09-09 21:38:55

好吧,假设您有 3 个 IEnumerable 变量,每个表一个。您的第一个查询将如下所示:

var records = from r1 in table1 where r1.GuestId == 63
              join r2 in table2 on r1.DepartmentId equals r2.DepartmentId
              select r2;

第二个查询与第一个查询几乎相同,只是连接表 3 而不是表 2。

var records = from r1 in table1 where r1.GuestId == 63
              join r3 in table3 on r1.ParentId equals r2.ParentId
              select r3;

Well, suppose you have 3 IEnumerable vars, one for each table. Your first query would be like this:

var records = from r1 in table1 where r1.GuestId == 63
              join r2 in table2 on r1.DepartmentId equals r2.DepartmentId
              select r2;

The second query is nearly the same as the first one, joining with table 3 instead of 2.

var records = from r1 in table1 where r1.GuestId == 63
              join r3 in table3 on r1.ParentId equals r2.ParentId
              select r3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文