使用linq返回数据表的组

时间:2010-10-18 10:05:09

标签: asp.net linq linq-to-sql

我有一个linq查询,它返回按照与当前文章匹配的标签数量排序的文章

例如

目前的文章有标签 - tag1,tag2,tag3

标记的文章1 - tag1,tag2,tag3 标记的文章2 - tag1,tag2

linq我有

 DataTable query = (from row in dt.AsEnumerable()  
                           let tags = row.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries)
                           let count = tags.Count(t => currenttags.Contains(t))
                           orderby count descending
                           select row).CopyToDataTable();

我想通过ClassName添加组,这是文章类型(文献,案例研究,下载等)

所以

group row by {row.Field<string>("ClassDisplayName")}

然而,当我将此添加到查询中时,我得到红色的sqiggles

   DataTable query = (from row in dt.AsEnumerable()    
                           group row by {row.Field<string>("ClassDisplayName")}
                           let tags = row.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries)
                           let count = tags.Count(t => currenttags.Contains(t))                             
                           orderby count descending                             
                           select row).CopyToDataTable();

任何想法我做错了什么?

这是目前仅适用于

的订单
DataTable dt = ArticleCollection(SqlClause.ToString());

        var seperator = new[] { ",", " " };
        var current = dr["Tags"].ToString();
        var currenttags = dr.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries);

        DataTable query = (from row in dt.AsEnumerable()                             
                           let tags = row.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries)
                           let count = tags.Count(t => currenttags.Contains(t))
                           orderby count descending
                          // group row by row.Field<string>("ClassDisplayName") into g
                           select row).CopyToDataTable();


        if (!DataHelper.DataSourceIsEmpty(query))
        {
            TagRepeaterOutter.DataSource = query;
            TagRepeaterOutter.DataBind();
        }

3 个答案:

答案 0 :(得分:1)

如果您尝试按计数排序,并且在每个count值中,按ClassDisplayName对行进行分组,您可以尝试:

DataTable query = (from row in dt.AsEnumerable()                             
                   let tags = row.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries)
                   let count = tags.Count(t => currenttags.Contains(t))
                   let displayName = row.Field<string>("ClassDisplayName")
                   orderby count descending, displayName
                   select row).CopyToDataTable();

答案 1 :(得分:1)

DataTable query = (from row in dt.AsEnumerable()
                   where (row.Field<string>("ClassDisplayName") == dr["ClassDisplayName"].ToString())
                   let tags = row.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries)
                   let count = tags.Count(t => currenttags.Contains(t))
                   orderby count descending
                   select row).CopyToDataTable();

答案 2 :(得分:0)

 IEnumerable<DataRow> types = (from row in dt.AsEnumerable()
                                      group row by row.Field<string>("ClassDisplayName") into g
                                      select g.FirstOrDefault());

        DataTable dtType = types.CopyToDataTable();

然后对内部嵌套转发器的另一个查询

DataTable query = (from row in dt.AsEnumerable()
                               where (row.Field<string>("ClassDisplayName") == dr["ClassDisplayName"].ToString())
                               let tags = row.Field<string>("Tags").Split(seperator, StringSplitOptions.RemoveEmptyEntries)
                               let count = tags.Count(t => currenttags.Contains(t))                                  
                               orderby count descending                              
                               select row).CopyToDataTable();

任何人都有更好的解决方案让我知道