class Program
{
static void Main(string[] args)
{
List<Test> tList = new List<Test>();
tList.Add(new Test(){
time=DateTime.Parse("2010-11-30"),
content="这是内容1"
});
tList.Add(new Test()
{
time = DateTime.Parse("2010-11-30"),
content = "这是内容2"
});
tList.Add(new Test()
{
time = DateTime.Parse("2010-11-30"),
content = "这是内容3"
});
tList.Add(new Test()
{
time = DateTime.Parse("2010-11-29"),
content = "这是内容4"
});
tList.Add(new Test()
{
time = DateTime.Parse("2010-11-29"),
content = "这是内容5"
});
List<Test> temp = tList.Distinct(new Comparer()).ToList<Test>();
foreach (var o in temp)
{
foreach (var p in tList)
{
if (p.time == o.time&&p.content!=o.content)
{
o.content += ","+p.content;
}
}
}
foreach (var o in temp)
{
Console.WriteLine(o.time+" "+o.content);
}
Console.Read();
}
}
public class Comparer : IEqualityComparer<Test>
{
#region IEqualityComparer<Test> Members
public bool Equals(Test x, Test y)
{
if (x == null || y == null)
return false;
return x.time == y.time;
}
public int GetHashCode(Test obj)
{
return obj.ToString().GetHashCode();
}
#endregion
}
public class Test
{
public DateTime time { get; set; }
public string content { get; set; }
}
Contains()
union它有一个功能是可以去除重复项的
mark