1

我需要按两个集合过滤查询。

我有一个 ID 列表,我需要用它来过滤 linq 查询

集合看起来像这样

<table1_id, table2_id>

和contect看起来像这样

idList = <1,10>,<2,12>

查询看起来像这样

Dim l = (From t1 In Table1
Join t2 In Table2 ON t1.table1_id Equals t2.table2_id
Where idList.Contains(idList.table1_id) and idlist.Contains(idList.table2_id))

问题是这就像一个交叉连接,所以我得到了所有的组合。

如何使两个集合过滤器像这样查询

Select * from
table1 t1 Inner join table2 t2 on t1.ID = t2.ID
where t1.ID in (1,2)
and t2.ID in (10, 12)

这是我的代码中的真实示例:

SQL查询:

    Select * from
dbo.v_MARKET_BUSINESS_ROLES mbs
Inner join dbo.SAP_COMPONENT brs
 on mbs.BUSINESS_ROLE_ID = brs.BUSINESS_ROLE_ID
and mbs.erd in ('1','2')
and brs.ID in (10,12)
where 
brs.market_id = 1

代码:

选择角色有 2 个 id 的 1 和 2,所以我只从列表中收集选定的角色

Dim businessRolesIDs = (From olc In ctx.organization_level_constraint
                                          Where selectedOrgLevels.Contains(olc.organization_level_id) _
                                                            AndAlso olc.market_id = marketId _
                                          AndAlso selectedRoles.Contains(olc.business_role_id)
                                          Select olc.business_role_id, olc.sap_component_id).Distinct.ToList


here I try to filter by the collection returned above

Dim marketBusinessRolesCollection = (From m In ctx.GetMarketBusinessRolesFromView(marketId) _
                                                    Join bs In ctx.business_role_sap_component On m.C_BUSINESS_ROLE_ID Equals bs.business_role_id _
                                                    Where (From item In businessRolesIDs Select item.business_role_id).Contains(m.C_BUSINESS_ROLE_ID)  _
                                                    And (From item2 In businessRolesIDs Select item2.sap_component_id).Contains(bs.sap_component_id)  _
                                                    And bs.market_id = marketId _
                                                    Select New BusinessRole With _
                                                      { 
                                                         .BusinessRoleId = m.ERD
                                                      }).Distinct.ToList
4

1 回答 1

0

您必须选择 ID 列表的键和值。我明白这个系列的构建就像一个List<KeyValuePair<int, int>>

所以你的代码应该是这样的

Dim l =
    From t1 In Table1
    Join t2 In Table2 ON t1.table1_id Equals t2.table2_id
    Where (From item In idList Select item.Key).Contains(t1.table1_id)
    And (From item In idList Select item.Value).Contains(t2.table2_id);
于 2012-10-04T08:25:30.863 回答