Group using linq return datatable
i have a linq query that returns Articles ordered by the number of tags that match the current article
e.g
current article has tags - tag1, tag2, tag3
tagged article 1 - tag1,tag2,tag3 tagged article 2 - tag1,tag2
linq i have is
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();
i want to add the group by ClassName which is the article type (literature, case study, download etc)
so would be
group row by {row.Field<string>("ClassDisplayName")}
however when i add this to the query i get red 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();
any ideas what i'm doing wrong?
This is the current one that works with just the order by
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, StringSplitOptio开发者_JS百科ns.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();
}
If you are trying to order by count, and within each value of count, group the rows by ClassDisplayName, you can try this:
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();
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();
IEnumerable<DataRow> types = (from row in dt.AsEnumerable()
group row by row.Field<string>("ClassDisplayName") into g
select g.FirstOrDefault());
DataTable dtType = types.CopyToDataTable();
and then another query on the inner nested repeater
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();
anyone have a better solution let me know
精彩评论