Sunday, November 9, 2008

The most simple way to group by in a list collection using .NET 2.0 (IEqualityComparer & IComparer) and .NET 3.5 (Linq) functions

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace WebApplication1
{
public class PhotoFeq
{
private string _PhotoName;

public string PhotoName
{
get { return _PhotoName; }
set { _PhotoName = value; }
}
private int _Count;

public int Count
{
get { return _Count; }
set { _Count = value; }
}

public PhotoFeq(string photoname, int count)
{
_PhotoName = photoname;
_Count = count;
}
}

public class PhotoTag
{
private int _PhotoID;

public int PhotoID
{
get { return _PhotoID; }
set { _PhotoID = value; }
}
private string _Tag;

public string Tag
{
get { return _Tag; }
set { _Tag = value; }
}

public PhotoTag(int photoid, string tag)
{
_Tag = tag;
_PhotoID = photoid;
}
}

public class PhotoTagEqualityComparer : IEqualityComparer<PhotoTag>
{
public bool Equals(PhotoTag x, PhotoTag y)
{
if (x.Tag == y.Tag )
{
return true;
}
else
{
return false;
}
}

public int GetHashCode(PhotoTag obj)
{
return base.GetHashCode();
}
}

public class PhotoTagComparer : IComparer<PhotoTag>
{
public int Compare(PhotoTag x, PhotoTag y)
{
if (x.Tag == y.Tag) { return 0; } else { return x.Tag.CompareTo(y.Tag); }
}

}




public partial class _Default : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
//output
//kiki 1
//gigi 2
//jojo 3
//sasa 3
//lala 6
//nini 6

List<PhotoTag> phototags = new List<PhotoTag>();
phototags.Add(new PhotoTag(121, "lala"));
phototags.Add(new PhotoTag(122, "lala"));
phototags.Add(new PhotoTag(123, "lala"));
phototags.Add(new PhotoTag(124, "lala"));
phototags.Add(new PhotoTag(125, "lala"));
phototags.Add(new PhotoTag(126, "lala"));

phototags.Add(new PhotoTag(127, "jojo"));
phototags.Add(new PhotoTag(128, "jojo"));
phototags.Add(new PhotoTag(129, "jojo"));


phototags.Add(new PhotoTag(120, "gigi"));
phototags.Add(new PhotoTag(1201, "gigi"));

phototags.Add(new PhotoTag(1202, "kiki"));


phototags.Add(new PhotoTag(1203, "sasa"));
phototags.Add(new PhotoTag(1204, "sasa"));
phototags.Add(new PhotoTag(1205, "sasa"));

phototags.Add(new PhotoTag(1206, "nini"));
phototags.Add(new PhotoTag(1207, "nini"));
phototags.Add(new PhotoTag(1208, "nini"));
phototags.Add(new PhotoTag(1209, "nini"));
phototags.Add(new PhotoTag(1211, "nini"));
phototags.Add(new PhotoTag(1212, "nini"));

//3.5 method
var feqTags = from n in phototags
group n by n.Tag into g
orderby g.Count()
select new { PhotoName = g.Key , Count = g.Count() };


foreach (var g in feqTags)
{
Response.Write(g.PhotoName + " " + g.Count + "<br/>");
}

//2.0 method
phototags.Sort(new PhotoTagComparer());

List<PhotoFeq> photofeqlist = new List<PhotoFeq>();

foreach (PhotoTag ptag in phototags)
{
if (photofeqlist.Exists(delegate(PhotoFeq pf) { return pf.PhotoName == ptag.Tag; }))
{
photofeqlist.Find(delegate(PhotoFeq pq) { return pq.PhotoName == ptag.Tag; }).Count += 1;
}
else
{
photofeqlist.Add(new PhotoFeq(ptag.Tag, 1));
}
}

//search object by comparison
int j = phototags.BinarySearch(new PhotoTag(1202, "kiki"), new PhotoTagComparer());
Response.Write(phototags[j].PhotoID);

//get distinct in list
List<PhotoTag> distinctPhotoTag = phototags.Distinct(new PhotoTagEqualityComparer()).ToList<PhotoTag>();


}
}
}

1 comment:

Anonymous said...

buy tramadol best site buy tramadol - buy tramadol lowest price