I have a list of products and every product have a string filed that is contain a list of tags that are concate with "#" character like this:
Tag1#Tage2#Tag3
I need to get all tags and order them by their number of their repeats.
I actually did this like this:
List<string> t = new List<string>();
var tags = (from p in db.Products
where p.Active
select p.Tags
).ToList();
foreach (var item in tags)
{
if (item == null)
continue;
var d = item.Split('#');
foreach (var item2 in d)
{
t.Add(item2);
}
}
var ans = t.GroupBy(p => new { id = p }).Select(g => new { id = g.Key.id, total = g.Count() }).OrderByDescending(g => g.total).ToList();
but im sure its not simple (and maybe optimized). Can someone help me to make this code simpler and better? for example with Linq statement etc..