Website development commonly used keywords (tag), generally need to get the most used tag, that is, popular words.
The usual idea is to save the keyword tag in a separate table and then identify multiple keywords in the other table based on a set of IDs. Because an article can select multiple tag, the query is rather troublesome.
So I used this method in development, of course, not much clever: to share the experience.
Store tag directly in the article's table, such as the tag field, tag is "C #" and ". Net", then the Tag field value of "C#/.net" is based on/symbol split tag, so that the database is stored in a string. When a single article is displayed, it can directly segment the array display, reduce the association of the table and improve the efficiency.
When you need to display the most recent buzzwords.
Query all the tag
Select tag from table
Use DataReader to add up the output. Then the delimiter is converted to a string, and the rest is sorted by the string, depending on how much the tag weighs. and return the number of duplicates, see Code. For easy viewing, I've written on an ASPX page.
Copy Code code as follows:
<%@ Page language= "C #"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "XXXX. BLL "%>
<%@ Import namespace= "XXXX. Model "%>
<%@ Import namespace= "XXXX. Dbutility "%>
<script runat= "Server" >
protected string stext;
protected void Page_Load (object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder ();
using (System.Data.SqlClient.SqlDataReader Rd = XXXX. DBUtility.SqlHelper.ExecuteReader (sqlhelper.connectionstring, System.Data.CommandType.Text, "Select Kinds +"/"from XXX ", NULL))
{
while (Rd. Read ())
{
Sb. Append (Rd. GetString (0));
}
}
Stext = sb. ToString ();
Toarraybysort (ToArray (stext, '/'));
}
<summary>
Converts a string to an array by separator
</summary>
<param name= "Sourcestring" > the string to be converted </param>
<param name= "Compart" > Separator </param>
<returns></returns>
Public ArrayList ToArray (string sourcestring, Char split)
{
Charenumerator CE = sourcestring. GetEnumerator ();
StringBuilder sb = new StringBuilder ();
ArrayList slist = new ArrayList ();
while (CE. MoveNext ())
{
if (CE. Current!= split)
{
Sb. Append (CE. Current);
}
Else
{
if (string. Empty = sb. ToString ()) continue;
Slist. ADD (sb.) ToString ());
Sb. Remove (0, sb.) ToString (). Length);
}
}
return slist;
}
public class Mycomparer:icomparer
{
int Icomparer.compare (object x, Object y)
{
Return (new CaseInsensitiveComparer ()). Compare (((Sortitem) y). Count, ((Sortitem) x). Count));
}
}
public class Sortitem
{
private string ItemName;
private int count;
Public Sortitem ()
{
}
public string ItemName
{
get {return itemname;}
set {itemname = value;}
}
public int Count
{
get {return count;}
set {count = value;}
}
}
Public system.collections.generic.ilist<sortitem> Toarraybysort (ArrayList slist)
{
Slist. Sort ();
ArrayList sortlist = new ArrayList ();
foreach (Object obj in Slist)
{
Sortitem Sitem = new Sortitem ();
Sitem.itemname = obj. ToString ();
Sitem.count = 1;
if (Sortlist.count = = 0) {sortlist.add (sitem); continue;}
if (obj. ToString () = = ((Sortitem) sortlist[sortlist.count-1]). ItemName)
{
Sitem.count = ((Sortitem) sortlist[sortlist.count-1]). Count + 1;
Sortlist.removeat (sortlist.count-1);
}
Sortlist.add (Sitem);
}
Mycomparer mycm = new Mycomparer ();
Sortlist.sort (MYCM);
system.collections.generic.ilist<sortitem> IList = new system.collections.generic.list<sortitem> ();
foreach (Object obj in Sortlist)
{
Ilist.add ((sortitem) obj);
Response.Write ((Sortitem) obj). ItemName + "-" + ((sortitem) obj). Count.tostring () + "<br/>");
}
return iList;
}
</script>