3

实际上,我得到了list of top 5 countires based on count这样的:

select top 5 COUNT(distinct FROM_EMAILID) as Count, 
FROM_COUNTRY from SURVEY_VISITORS  
where TEMPLATE_ID=79 and FROM_COUNTRY<>'undefined' 
group by FROM_COUNTRY order by COUNT desc

现在,我需要转换成 Linq,但无法做到。

我曾尝试使用这样的子查询来获得前 1 个国家/地区。但是对于前 5 个国家/地区,我有点困惑:

 var innerQuery = (from t in VDC.SURVEY_VISITORS
                   group t by new
                   {
                      t.FROM_COUNTRY
                   } into g
                   orderby
                   g.Count() descending
                   select new
                   {
                     VisitorCount = (Int64?)g.Count(),
                     Country = g.Key.FROM_COUNTRY
                   }).FirstOrDefault();

   var result = (from xx in VDC.SURVEY_VISITORS
                  where ((innerQuery.Country.Contains(xx.FROM_COUNTRY)) 
                   && xx.TEMPLATE_ID == 79)
                   select new
                    {
                       xx.FROM_COUNTRY,
                       xx.FROM_EMAILID
                     }).Distinct().ToList();

我的结果应该是: 在此处输入图像描述

任何帮助将不胜感激。

4

2 回答 2

2

不确定,如果OrderBy会吞下那个复杂的表达式,但试试这个:

SurveyVisitors
    .Where(x => x.TemplateId = 79 && x.FromCountry != "undefined")
    .GroupBy(x => x.FromCountry)
    .OrderByDescending(x => x.Select(y => y.FromEmailId).Distinct().Count())
    .Take(5)
    .Select(new => {
        Count = x.Select(y => y.FromEmailId).Distinct().Count(),
        FromCountry = x.Key
    })
于 2013-10-03T07:32:44.390 回答
0

这对我来说很完美。

var query = (from xx in VDC.SURVEY_VISITORS
             where xx.TEMPLATE_ID == tempid
             group xx by new { xx.FROM_COUNTRY } into g
             select new
             {
                Count = g.Select(act => act.FROM_EMAILID).Distinct().Count(),
                g.Key.FROM_COUNTRY
             }).OrderByDescending(x => x.Count).Take(5);
于 2013-10-03T07:40:08.503 回答