如何使用 EF Core 以编程方式添加 JOIN 以从列表中查询
我正在尝试使用EF Core从数据库查询数据,但是风景对我来说有点复杂。我将尝试清楚并综合我想完成的目标。
涉及三个表:
- table work_to_do-列:ID,描述
- 表param_definitions_for_work-列 - 列:id,名称
- 表param_values_for_work-列 - 列:work_to_do_id,param_definition_id,value value
让我们说这些表具有下面的类。
public class WorkToDo
{
public int Id { get; set; }
public string Description { get; set; }
}
public class ParamDefinition
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ParamValue
{
public int WorkToDoId { get; set; }
public int ParamDefinitionId { get; set; }
public string Value { get; set; }
}
我有一个带有paramdefinitionID的Paramvalue项目的列表,但没有工作todoid。
我想查询所有与Paramvalue项目相匹配的worktodo项目,考虑到 all paramvalue项目,而不仅仅是其中的任何一个。
让我用每个表上的示例记录解释:
work_to_do
ID | 描述 |
---|---|
1 | 工作示例A |
2 | 工作示例B |
Param_definitions_for_work
ID ID | 名称名称 |
---|---|
101 | 参数定义x |
102 | param定义y |
103 | param定义w |
104 | param定义w |
104 | param param param param定义 + |
param_values_for_for_work
work_do_do_do_do_do | param_definition_id param_definition_id param_definition_id param_definition_id param_definition_id param_definition_id | param_dem_definition_d |
---|---|---|
param_definition_id | param_definition_id | param值j |
1 | 102 | param值k |
2 | 103 | param值l |
2 | 104 | param值m |
2 | 105 | param value n |
因此,假设我的paramvalues列表有两个项目:paramdefinitionId = 101,value =“ param值j” << /code>和
paramdefinitionID = 102,value =“ param value k”
。我想检索ID = 1的工作
多
- 。
- 托 =“ param value m”
paramdefinitionid = 105,value =“ param value n”
,
然后我希望我的查询以id = 2的worktodo检索。
请注意,ParamValues列表的大小是可变的!
我想说我已经尝试了解决方案,但事实是我什至不知道如何开始。我在网上搜索了,但没有运气。
我只想使用SQL如何做到这一点:
SELECT DISTINCT WORK_TO_DO.ID, WORK_TO_DO.DESCRIPTION
FROM WORK_TO_DO
INNER JOIN PARAM_VALUES_FOR_WORK PV1 ON PV1.WORK_TO_DO_ID = WORK_TO_DO.ID
INNER JOIN PARAM_VALUES_FOR_WORK PV2 ON PV2.WORK_TO_DO_ID = WORK_TO_DO.ID
(... Adding as many INNER JOINs as needed based on list of ParamValues)
INNER JOIN PARAM_VALUES_FOR_WORK PVX ON PVX.WORK_TO_DO_ID = WORK_TO_DO.ID
WHERE PV1.PARAM_DEFINITION_ID = :ParamValues[0].ParamDefinitionId
AND PV1.VALUE = :ParamValues[0].Value
AND PV2.PARAM_DEFINITION_ID = :ParamValues[1].ParamDefinitionId
AND PV2.VALUE = :ParamValues[1].Value
(... Adding as many conditions as needed based on list of ParamValues)
AND PVX.PARAM_DEFINITION_ID = :ParamValues[X].ParamDefinitionId
AND PVX.VALUE = :ParamValues[X].Value
基本上我想根据我的参数列表将加入和过滤器添加到查询中。我该怎么做?
I am trying to query data from database using EF Core, but the scenery is a bit complicated for me. I will try to be clear and synthesize what I want to accomplish.
There are three tables involved:
- Table WORK_TO_DO - Columns: ID, DESCRIPTION
- Table PARAM_DEFINITIONS_FOR_WORK - Columns: ID, NAME
- Table PARAM_VALUES_FOR_WORK - Columns: WORK_TO_DO_ID, PARAM_DEFINITION_ID, VALUE
Let's say these tables have their classes as below.
public class WorkToDo
{
public int Id { get; set; }
public string Description { get; set; }
}
public class ParamDefinition
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ParamValue
{
public int WorkToDoId { get; set; }
public int ParamDefinitionId { get; set; }
public string Value { get; set; }
}
I have a list of ParamValue items with ParamDefinitionId and Value populated, but without WorkToDoId.
I want to query all WorkToDo items that match the ParamValue items, considering all the ParamValue items and not just any of them.
Let me explain with example records on each table:
WORK_TO_DO
ID | DESCRIPTION |
---|---|
1 | Work Example A |
2 | Work Example B |
PARAM_DEFINITIONS_FOR_WORK
ID | NAME |
---|---|
101 | Param Definition X |
102 | Param Definition Y |
103 | Param Definition W |
104 | Param Definition Z |
105 | Param Definition + |
PARAM_VALUES_FOR_WORK
WORK_TO_DO_ID | PARAM_DEFINITION_ID | VALUE |
---|---|---|
1 | 101 | Param Value J |
1 | 102 | Param Value K |
2 | 103 | Param Value L |
2 | 104 | Param Value M |
2 | 105 | Param Value N |
So, let's say my list of ParamValues has two items: ParamDefinitionId = 101, Value = "Param Value J"
and ParamDefinitionId = 102, Value = "Param Value K"
. I would like to retrieve the WorkToDo of Id = 1.
If my list of ParamValues had, instead, three items:
ParamDefinitionId = 103, Value = "Param Value L"
ParamDefinitionId = 104, Value = "Param Value M"
ParamDefinitionId = 105, Value = "Param Value N"
Then I would like my query to retrieve the WorkToDo of Id = 2.
Note that the size of ParamValues list is variable!
I'd like to say that I have tried a solution, but the truth is I don't even know how to begin. I've searched on the web but had no luck.
I only have an idea of how I would do this using SQL:
SELECT DISTINCT WORK_TO_DO.ID, WORK_TO_DO.DESCRIPTION
FROM WORK_TO_DO
INNER JOIN PARAM_VALUES_FOR_WORK PV1 ON PV1.WORK_TO_DO_ID = WORK_TO_DO.ID
INNER JOIN PARAM_VALUES_FOR_WORK PV2 ON PV2.WORK_TO_DO_ID = WORK_TO_DO.ID
(... Adding as many INNER JOINs as needed based on list of ParamValues)
INNER JOIN PARAM_VALUES_FOR_WORK PVX ON PVX.WORK_TO_DO_ID = WORK_TO_DO.ID
WHERE PV1.PARAM_DEFINITION_ID = :ParamValues[0].ParamDefinitionId
AND PV1.VALUE = :ParamValues[0].Value
AND PV2.PARAM_DEFINITION_ID = :ParamValues[1].ParamDefinitionId
AND PV2.VALUE = :ParamValues[1].Value
(... Adding as many conditions as needed based on list of ParamValues)
AND PVX.PARAM_DEFINITION_ID = :ParamValues[X].ParamDefinitionId
AND PVX.VALUE = :ParamValues[X].Value
Basically I want to add JOINs and filters to the query based on my list of ParamValues. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Use FilterByItems extension and you can generate desired query: