使用DataTable和List <t> </t>加入和分组

时间:2014-06-16 08:51:12

标签: c# sql linq

我试图执行以下linq操作。这里表示为SQL:

SELECT 
    CAMPAIGN,
    ADGROUPID,
    CLICKS,
    TOTALCONV,
    COST,
    COSTCONVCLICK,
    KEYWORD,
    COUNT(*) AS APARICIONES
FROM 
    ADWORDSSEARCH
GROUP BY CAMPAIGN,adgrOUPID,CLICKS,TOTALCONV,COST,COSTCONVCLICK,KEYWORD
ORDER BY CLICKS DESC

我使用了以下linq表达式:

var filtro =(from r in newTable.AsEnumerable()
             group r by new { 
                                camp=r.Field<string>("CAMPAIGN"), 
                                keyw= r.Field<string>("KEYWORD")
                            }
             into grouping
             select new
             {
                 grouping.Key.camp,
                 grouping.Key.keyw,
                 Key = grouping.Key,
                 NumberGroup = grouping.Count()
             }).ToList()
               .OrderByDescending(t=>t.NumberGroup);

但问题是newTable是一个数据表,它有一个名为AdGroupID的数字字段,我需要与List进行连接,用它的描述替换AdGroupID的数字ID。这是我到目前为止所尝试的:

var filtro = from r in newTable.AsEnumerable()
             join anuncios in adslist.AsEnumerable() 
             on r.Field<string>("ADGROUPSID") equals anuncios.id.toString() 
             into grouping
             select new
             {
                 Campaign = r.Field<string>("CAMPAIGN"),
                 AdsName = anuncios.name ???? ->Here is the problem
                 keyword = r.Field<string>("KEYWORD"),
                 Counting = grouping.Count()
             };

1 个答案:

答案 0 :(得分:1)

我使用from d in grouping.DefaultIfEmpty()

开始工作
               var filtro = (from r in newTable.AsEnumerable()
                             join anuncios in adslist.AsEnumerable() on r.Field<string>("ADGROUPID") equals anuncios.id.ToString() into grouping
                             from d in grouping.DefaultIfEmpty()
                             select new
                                 {

                                     Campaign = r.Field<string>("CAMPAIGN"),
                                     AdName = d.name,
                                     Clicks = r.Field<string>("CLICKS"),
                                     TotalConv = r.Field<string>("TotalConvValue"),
                                     Cost = r.Field<string>("Cost"),
                                     CostConvClick = r.Field<string>("CostConvertedClick"),
                                     keyword = r.Field<string>("KEYWORD"),
                                     Counting = grouping.Count()
                                 }).ToList().OrderByDescending(t => t.Clicks); 

它也使用group by然后join,没有from子句:

   List<AdwordsClass> filtro = (from r in newTable.AsEnumerable()
                                   group r by new{ camp=r.Field<string>("CAMPAIGN"), id=r.Field<string>("adgroupid"), keyw= r.Field<string>("KEYWORD")} into grouping
                                  join anuncios in adslist.AsEnumerable() on grouping.FirstOrDefault().Field<string>("ADGROUPID") equals anuncios.id.ToString()
                                                 select new AdwordsClass()
                                  {
                                      CampaignName = grouping.Key.camp,
                                      CampaignId   =  Convert.ToInt64(grouping.FirstOrDefault().Field<string>("CAMPAIGNID")),
                                      AdsGroupID = grouping.Key.id,
                                      KeyWord = grouping.Key.keyw,
                                      AdsGroupName = anuncios.name,
                                      clicks = grouping.FirstOrDefault().Field<string>("CLICKS"),
                                      TotalConv = grouping.FirstOrDefault().Field<string>("TotalConvValue"),
                                      Cost =grouping.FirstOrDefault().Field<string>("Cost"),
                                      CostConvClick =grouping.FirstOrDefault().Field<string>("CostConvertedClick"),
                                      Counting = grouping.Count()
                                  }).ToList().OrderByDescending(t => t.clicks).ToList();