c# lambda distinct
在写程序的时候会遇见这样的问题,那就是去重,有什么方法更快呢。
当去重时,首先想到的是自己写代码,代码大概如下:
private static void distinctListIntTest()
{
Console.WriteLine("未去重");
List<int> list = new List<int>() { 1, 2, 3, 4, 1, 2, 3, 4 };
foreach (int inst in list)
{
Console.WriteLine(inst);
}
Console.WriteLine("=======================");
Console.WriteLine("去重后");
List<int> list1 = new List<int>();
foreach (int inst in list)
{
if (!list1.Contains(inst))
{
list1.Add(inst);
}
}
foreach (int inst in list1)
{
Console.WriteLine(inst);
}
}
这段代码确实能实现我们想要的效果,结果如下:
这段代码虽然能实现,但是要写很多代码,用起来不方便。有没有更好的办法呢,办法是有的,那就是lambda表达式的distinct方法,代码如下:
private static void distinctListInt()
{
Console.WriteLine("未去重");
List<int> list = new List<int>() { 1, 2, 3, 5, 1, 2, 3, 5 };
foreach (int inst in list)
{
Console.WriteLine(inst);
}
Console.WriteLine("=======================");
Console.WriteLine("去重后");
list = list.Distinct().ToList();
foreach (int inst in list)
{
Console.WriteLine(inst);
}
}
执行结果如下:
当简单的类型可以了,那如果是一个对象怎么办,用自己写的方法代码如下:
首先声明一个对象
public class user
{
public string Name { get; set; }
public int Age { get; set; }
}
去重的方法
private static void distinctListEntityTest()
{
Console.WriteLine("未去重");
List<user> list = new List<user>();
user user = new user() { Name = "张三", Age = 13 };
list.Add(user);
user = new user() { Name = "张三", Age = 13 };
list.Add(user);
user = new user() { Name = "李四", Age = 14 };
list.Add(user);
user = new user() { Name = "李四", Age = 14 };
list.Add(user);
user = new user() { Name = "王五", Age = 15 };
list.Add(user);
user = new user() { Name = "王五", Age = 15 };
list.Add(user);
foreach (user inst in list)
{
Console.WriteLine(inst.Name);
}
Console.WriteLine("=======================");
Console.WriteLine("去重后");
List<user> list1 = new List<user>();
foreach (user inst in list)
{
bool isbool = true;
foreach (user inst1 in list1)
{
if (inst1.Name.Equals(inst.Name))
{
isbool = false;
break;
}
}
if (isbool)
{
list1.Add(inst);
}
}
foreach (user inst in list1)
{
Console.WriteLine(inst.Name);
}
}
去重的方法【lambda表达式】
private static void distinctListEntityTest()
{
Console.WriteLine("未去重");
List<user> list = new List<user>();
user user = new user() { Name = "张三", Age = 13 };
list.Add(user);
user = new user() { Name = "张三", Age = 13 };
list.Add(user);
user = new user() { Name = "李四", Age = 14 };
list.Add(user);
user = new user() { Name = "李四", Age = 14 };
list.Add(user);
user = new user() { Name = "王五", Age = 15 };
list.Add(user);
user = new user() { Name = "王五", Age = 15 };
list.Add(user);
foreach (user inst in list)
{
Console.WriteLine(inst.Name);
}
Console.WriteLine("=======================");
Console.WriteLine("去重后");
List<user> list1=new List<user>();
foreach (user inst in list)
{
if (list1.Count(u => u.Name.Equals(inst.Name))>0)//这句用了lambda表达式
{
continue;
}
list1.Add(inst);
}
foreach (user inst in list1)
{
Console.WriteLine(inst.Name);
}
}
这两种方法执行结果如下:
下面就用lambda表达式的distinct()方法代码如下
private static void distinctListEntity()
{
Console.WriteLine("未去重");
List<user> list = new List<user>();
user user = new user() { Name = "张三", Age = 13 };
list.Add(user);
user = new user() { Name = "张三", Age = 13 };
list.Add(user);
user = new user() { Name = "李四", Age = 14 };
list.Add(user);
user = new user() { Name = "李四", Age = 14 };
list.Add(user);
user = new user() { Name = "王五", Age = 15 };
list.Add(user);
user = new user() { Name = "王五", Age = 15 };
list.Add(user);
foreach (user inst in list)
{
Console.WriteLine(inst.Name);
}
Console.WriteLine("=======================");
Console.WriteLine("去重后");
list = list.Distinct(new userCompare()).ToList();
foreach (user inst in list)
{
Console.WriteLine(inst.Name);
}
}
}
class userCompare : IEqualityComparer<user>
{
#region IEqualityComparer<user> Members
public bool Equals(user x, user y)
{
return x.Name.Equals(y.Name);
}
public int GetHashCode(user obj)
{
return obj.Name.GetHashCode();
}
#endregion
}
运行结果如下:
最后的lambda表达式的distinct()方法只是抛砖引玉,distinct()方法好像有更简单的实现方法,欢迎为我解惑。