0

实际上我正在尝试将我的SQLQuery 转换为Linq. 我的查询包含 3 个表:

我的查询:

select ST.TEMPLATE_ID,ST.TEMPLATE_NAME,ST.CREATED_DATE
,count(distinct SQ.QUESTION_ID) as NOQ
,(
select count(*) from SURVEY_EMAIL_BLAST SEB 
where SEB.TEMPLATE_ID=ST.TEMPLATE_ID
and SEB.STATUS='Delivered'
) as [Sent]
,(
select count(*) from SURVEY_EMAIL_BLAST SEB 
where SEB.TEMPLATE_ID=ST.TEMPLATE_ID
and (SEB.EMAIL_RESULT_TEXT='Viewed' or seb.EMAIL_RESULT_TEXT='Completed')
) as [View]
,(
select count(*) from SURVEY_EMAIL_BLAST SEB 
where SEB.TEMPLATE_ID=ST.TEMPLATE_ID
and SEB.EMAIL_RESULT_TEXT='Completed'
) as [Reply]
from SURVEY_TEMPLATE ST left outer join SURVEY_QUESTION  SQ
on ST.TEMPLATE_ID=SQ.TEMPLATE_ID
group by ST.TEMPLATE_ID,ST.TEMPLATE_NAME,ST.CREATED_DATE
order by ST.TEMPLATE_ID

我的结果是:

在此处输入图像描述

我的 linq 查询是这样的:

var data111 = (from st in VDC.SURVEY_TEMPLATE
               join sq in VDC.SURVEY_QUESTION on new { TEMPLATE_ID = st.TEMPLATE_ID } equals new { TEMPLATE_ID = (Int64)sq.TEMPLATE_ID } into fg
               from z in fg.DefaultIfEmpty()
               group st by new
               {
                  st.TEMPLATE_ID,
                  st.TEMPLATE_NAME,
                  st.CREATED_DATE,                                           
               } into g
               orderby
               g.Key.TEMPLATE_ID
               select new
               {
                  TEMPLATE_ID = (Int64?)g.Key.TEMPLATE_ID,
                  g.Key.TEMPLATE_NAME,
                  CREATED_DATE = (DateTime?)g.Key.CREATED_DATE,
                  Sent = (Int64?)
                  (from seb in VDC.SURVEY_EMAIL_BLAST
                   where
                  seb.SURVEY_TEMPLATE.TEMPLATE_ID == g.Key.TEMPLATE_ID &&
                    seb.STATUS == "Delivered"
                    select new
                    {
                      seb
                    }).Count(),
                  View = (Int64?)
                  (from seb in VDC.SURVEY_EMAIL_BLAST
                    where
                    seb.SURVEY_TEMPLATE.TEMPLATE_ID == g.Key.TEMPLATE_ID && (
                    seb.EMAIL_RESULT_TEXT == "Viewed" ||
                    seb.EMAIL_RESULT_TEXT == "Completed")
                    select new
                     {
                       seb
                     }).Count(),
                    Reply = (Int64?)
                     (from seb in VDC.SURVEY_EMAIL_BLAST
                       where
                       seb.SURVEY_TEMPLATE.TEMPLATE_ID == g.Key.TEMPLATE_ID &&
                       seb.EMAIL_RESULT_TEXT == "Completed"
                       select new
                        {
                          seb
                         }).Count()
                   }).ToList();

现在我的结果是:

在此处输入图像描述

我无法显示NOQ列。谁能帮我?


4

1 回答 1

1

首先,添加zgroup by源码中:

           group new { st , z } by new
           {
              st.TEMPLATE_ID,
              st.TEMPLATE_NAME,
              st.CREATED_DATE,                                           
           } into g

然后用它NOQ进入select

NOQ = g.Select(x => x.z.QUESTION_ID).Distinct().Count()

或者只是替换stz,因为您st以后不会在任何地方使用:

           group z by new
           {
              st.TEMPLATE_ID,
              st.TEMPLATE_NAME,
              st.CREATED_DATE,                                           
           } into g

但它也需要改变select

NOQ = g.Select(x => x.QUESTION_ID).Distinct().Count()

老实说:您的查询看起来很糟糕。你确定不能做得更好吗?

于 2013-09-13T06:36:36.043 回答