Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用联接对Linq进行Sql查询_C#_Asp.net_.net_Linq_Linq To Entities - Fatal编程技术网

C# 使用联接对Linq进行Sql查询

C# 使用联接对Linq进行Sql查询,c#,asp.net,.net,linq,linq-to-entities,C#,Asp.net,.net,Linq,Linq To Entities,实际上,我正在尝试将我的SQL查询转换为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'

实际上,我正在尝试将我的
SQL
查询转换为
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
列。 有人能帮我吗



首先,通过来源将
z
添加到
组中:

           group new { st , z } by new
           {
              st.TEMPLATE_ID,
              st.TEMPLATE_NAME,
              st.CREATED_DATE,                                           
           } into g
然后使用它在
选择中获取
NOQ

NOQ = g.Select(x => x.z.QUESTION_ID).Distinct().Count()
或者将
st
替换为
z
,因为您以后不会在任何地方使用
st

           group z by new
           {
              st.TEMPLATE_ID,
              st.TEMPLATE_NAME,
              st.CREATED_DATE,                                           
           } into g
但它也需要更改
选择

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

老实说,你的问题看起来很糟糕。你确定不能做得更好吗?

当我用new编写组new{st,sq}by new:sq不是intellisense的,错误是无法将lambda表达式转换为类型“不等式比较器”@RealSteel OK,我错过了
DefaultIfEmpty
部分。尝试使用
z
而不是
sq
。即使我也这样认为:)我需要更改我的查询:)