如何使用 EF Core 以编程方式添加 JOIN 以从列表中查询

发布于 2025-01-17 20:26:09 字数 3322 浏览 0 评论 0原文

我正在尝试使用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
102param定义y
103param定义w
104param定义w
104param param param param定义 +

param_values_for_for_work

work_do_do_do_do_doparam_definition_id param_definition_id param_definition_id param_definition_id param_definition_id param_definition_idparam_dem_definition_d
param_definition_idparam_definition_idparam值j
1102param值k
2103param值l
2104param值m
2105param 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

IDDESCRIPTION
1Work Example A
2Work Example B

PARAM_DEFINITIONS_FOR_WORK

IDNAME
101Param Definition X
102Param Definition Y
103Param Definition W
104Param Definition Z
105Param Definition +

PARAM_VALUES_FOR_WORK

WORK_TO_DO_IDPARAM_DEFINITION_IDVALUE
1101Param Value J
1102Param Value K
2103Param Value L
2104Param Value M
2105Param 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 技术交流群。

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

发布评论

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

评论(1

爱给你人给你 2025-01-24 20:26:09

使用

var requiredCount = ParamValues.Count();

var query = context.WorkToDo
    .Where(w => context.ParamValue
        .Where(pv = pv.WorkToDoId == w.Id)
        .FilterByItems(ParamValues, (pv, v) => pv.ParamDefinitionId == v.ParamDefinitionId && pv.Value == v.Name, true)
        .Count() >= requiredCount
    );

Use FilterByItems extension and you can generate desired query:

var requiredCount = ParamValues.Count();

var query = context.WorkToDo
    .Where(w => context.ParamValue
        .Where(pv = pv.WorkToDoId == w.Id)
        .FilterByItems(ParamValues, (pv, v) => pv.ParamDefinitionId == v.ParamDefinitionId && pv.Value == v.Name, true)
        .Count() >= requiredCount
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文