保存实体框架查询以供以后使用的方法 - 报告创建

时间:2018-02-12 21:09:42

标签: c# entity-framework

在我们公司,我们为使用我们服务的客户创建自定义报告。我们收集的数据是与会者在活动中的联系人数据(名字,姓氏,公司,电子邮件等),但也有捕获的动态数据。我们收集数据的每个事件,都有针对活动和公司定制的调查。

例如,在第123项活动中,可能会有一个包含2个问题的调查。 Q1可能是一个开放的问题/自由文本,Q2将是多项选择(从我们定义的选项列表中选择n)。

要对此类报告进行编码,它目前是我们需要的手动流程

1. Filter on all contact data for this event and the specific client
2. Create a temporary table with standard columns (first name, last name, etc)
3. But also with custom columns for however many survey questions we have (2)
4. Populate the temp table with the data from step 1
5. Use a set based update to get survey responses for each question
6. Format the report into the required format (order of columns)

目前这是在存储过程中完成的,这是耗时的,容易出错且乏味的。

有没有办法

  • 以某种方式保存EF查询(比如db.Events join ....中的事件),能够检索并执行吗?
  • 与上述类似,保存输出列的顺序,并在报告运行时反映出来

目前我们在sprocs中这样做,但理想情况下我们希望在报告生成工具中使用EF来做到这一点。

编辑:这是报告所需的SQL的精简版本。

-- Step 1. Filter lead data into temp table. Let's say there are 10 leads, and 8 have survey responses for the 2 questions.
SELECT leadID,FirstName,LastName,eMail
INTO #leads
FROM dbo.leads
WHERE eventid = 123 and companyid = 456

-- Step 2 and 3. Create a temporary table with standard contact fields plus survey questions.
CREATE TABLE #Data (
    LeadId NVARCHAR(50) PRIMARY KEY,
    FirstName NVARCHAR(100),
    LastName NVARCHAR(100),
    EmailAddress NVARCHAR(100),
    [Question1] NVARCHAR(1000),
    [Question2] NVARCHAR(1000)
)

-- Step 4. Populate temp table.
INSERT INTO #Data (LeadId,
    FirstName,
    LastName,
    EmailAddress) 
SELECT leadid,
    FirstName,
    LastName,
    Email
FROM #leads L

-- Step 5. Use a set based update to get survey responses for each question. 8 leads in each update statement would be updated as 2 leads did not answer the survey.
UPDATE TTARGET SET
Question1 = OA.Title
FROM #Data TTARGET
JOIN answerset answers on TTARGET.LeadId = answers.leadid
JOIN que.answer A on answers.answersetid = A.answersetid
JOIN que.optionalanswer OA ON A.possibleAnswerId = OA.optionalAnswerId
WHERE A.questionid IN ('3A2FFD77-BF37-4580-938A-FF234395B602') AND A.Active = 1

UPDATE TTARGET SET
Question2 = OA.Title
FROM #Data TTARGET
JOIN answerset answers on TTARGET.LeadId = answers.leadid
JOIN que.answer A on answers.answersetid = A.answersetid
JOIN que.optionalanswer OA ON A.possibleAnswerId = OA.optionalAnswerId
WHERE A.questionid IN ('75C5FA00-439F-4DA9-9662-9C683A0E6719') AND A.Active = 1

-- Step 6. Format the report. 
SELECT
    FirstName,
    LastName
    EmailAddress,
    LeadId,
    Question1 AS 'the title of the first question',
    Question2 AS 'the title of the second question'
FROM #Data

0 个答案:

没有答案