-1

我需要通过这个视图模型在视图中创建一个表:

public class ApplicationContentViewModel
{
    public BPMSPARS.Models.MySql.application application {get; set;}
    public BPMSPARS.Models.MySql.content content { get; set; }
    public BPMSPARS.Models.MySql.app_delegation app_delegation { get; set; }
}

但是创建新表的查询非常复杂。我在 MySQL 中使用了这个查询,使用它可以得到正确的结果。

SELECT APP_UID, (SELECT CON_VALUE FROM content WHERE CON_CATEGORY = 'PRO_TITLE' AND CON_ID = 
(SELECT PRO_UID from app_delegation WHERE del_thread_status='open' and USR_UID = '00000000000000000000000000000001' AND APP_UID = '9134216305aaaea1b67c4e2096663219')) AS TASK_NAME, 
(SELECT CON_VALUE FROM content WHERE CON_CATEGORY = 'TAS_TITLE' AND CON_ID = 
(SELECT TAS_UID from app_delegation WHERE del_thread_status='open' and USR_UID = '00000000000000000000000000000001' AND APP_UID = '9134216305aaaea1b67c4e2096663219')) AS PROCESS_NAME FROM app_delegation 
WHERE del_thread_status='open' and USR_UID = '00000000000000000000000000000001' AND APP_UID = '9134216305aaaea1b67c4e2096663219'

但是,我必须在 MVC 中的 linq 或 EF 中转换此查询。

如何在实体框架查询中编写此查询?以及如何在视图中显示结果?

4

1 回答 1

0

您的 SQL 查询对我来说似乎(非常)特别,因为它非常多余。我将假设子查询返回单个值并使用 LINQ 强制执行它。

首先,我通过以下方式提取了公共子查询app_delegation

var USR_APP_Delegation = from a in app_delegation
                         where a.del_thread_status == "open" &&
                               a.USR_UID == "00000000000000000000000000000001" &&
                               a.APP_UID == "9134216305aaaea1b67c4e2096663219"
                         select a;

在 LINQ 中,很容易将两个 UID 查询合并为一个查询:

var UIDs = (from a in USR_APP_Delegation
            select new { a.PRO_UID, a.TAS_UID })
           .Single();

现在您可以执行名称子查询:

var TASK_NAME = (from c in content
                 where c.CON_CATEGORY == "PRO_TITLE" &&
                 c.CON_ID == UIDs.PRO_UID
                 select c.CON_VALUE)
                .Single();

var PROCESS_NAME = (from c in content
                    where c.CON_CATEGORY == "TAS_TITLE" &&
                    c.CON_ID == UIDs.TAS_UID
                    select c.CON_VALUE)
                   .Single();

然后您可以将所有查询放在一起以获得最终结果:

var ans = (from a in USR_APP_Delegation
           select new {
               a.APP_UID,
               TASK_NAME,
               PROCESS_NAME
           })
          .Single();

同样,这很明显,APP_UID当您确切知道它是什么时,您的 eg 返回,并且您正在组合TASK_NAMEPROCESS_NAME进入查询,但没有真正的优势。

我建议使用joinagainstcontent进行更易于理解的查询(即使在 SQL 中)并使返回的内容更清晰:

var names = from a in app_delegation
            join cpro in content on new { CON_ID = a.PRO_UID, CON_CATEGORY = "PRO_TITLE" } equals new { cpro.CON_ID, cpro.CON_CATEGORY }
            join ctas in content on new { CON_ID = a.PRO_UID, CON_CATEGORY = "TAS_TITLE" } equals new { ctas.CON_ID, ctas.CON_CATEGORY }
            where a.del_thread_status == "open" &&
                  a.USR_UID == "00000000000000000000000000000001" &&
                  a.APP_UID == "9134216305aaaea1b67c4e2096663219"
            select new {
                a.APP_UID,
                Task_Name = ctas.CON_VALUE,
                Process_Name = cpro.CON_VALUE
            };
于 2018-03-19T19:18:49.347 回答