LINQ如何去掉重复记录 id n1 n2 n31 c1 c2 c32 c2 c3 c13 c3 c1 c24 c4 c1 c51,2,3行都只有c1,c2,c3虽然位置不一下,但咱们都把它当成重复的.要怎么能把它删除掉? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Collections.Generic;using System.Linq;class Line{ public int id { get; set; } public string n1 { get; set; } public string n2 { get; set; } public string n3 { get; set; }}class Tools:IEqualityComparer<Line>{ private string MakeSort(ref Line x) { string tmp; if(x.n1.CompareTo(x.n2)<0) { tmp = x.n1; x.n1 = x.n2; x.n2 = tmp; } if (x.n2.CompareTo(x.n3) < 0) { tmp = x.n2; x.n2 = x.n3; x.n3 = tmp; } if (x.n1.CompareTo(x.n2) < 0) { tmp = x.n1; x.n1 = x.n2; x.n2 = tmp; } return x.n1 + x.n2 + x.n3; } #region IEqualityComparer<Line> 成员 public bool Equals(Line x, Line y) { return MakeSort(ref x) == MakeSort(ref y); } public int GetHashCode(Line obj) { return MakeSort(ref obj).GetHashCode(); } #endregion}class MyClass{ static void Main(string[] args) { //准备 Line[] list = { new Line{ id = 1, n1 = "c1", n2 = "c2", n3 = "c3" }, new Line{ id = 2, n1 = "c2", n2 = "c3", n3 = "c1" }, new Line{ id = 3, n1 = "c3", n2 = "c1", n3 = "c2" }, new Line{ id = 4, n1 = "c4", n2 = "c1", n3 = "c5" }, }; //执行 Line[] listOk = list.Distinct(new Tools()).ToArray(); //验证 foreach (var x in listOk) { Console.WriteLine(x.id + " " + x.n1 + " " + x.n2 + " " + x.n3); } //1 c3 c2 c1 //4 c5 c4 c1 Console.ReadKey(); }} 跟SQL语句有点相似。呵呵、。 C# serialport 从串口接受16进制数据的问题 Reader值放进Datatable的问题。 该字符串未被识别为有效的 DateTime。 关于VS使用过程中遇到的问题 求C#中控件自动排列,随着窗口大小变化,多个相同的控件自动排列 PictureBox绑定ImagesList问题 继承构造函数 关于条形码的问题。急。。 讨论:数据库安全性的问题。希望写过类似数据库同时被c/s内网用户、b/s公网用户调用程序的高手参加! 800分求.net平台实现网上审批的技术方案 Dropdownlist 怎么绑定Treeview 获取路径问题
using System.Collections.Generic;
using System.Linq;class Line
{
public int id { get; set; }
public string n1 { get; set; }
public string n2 { get; set; }
public string n3 { get; set; }
}class Tools:IEqualityComparer<Line>
{
private string MakeSort(ref Line x)
{
string tmp;
if(x.n1.CompareTo(x.n2)<0)
{
tmp = x.n1;
x.n1 = x.n2;
x.n2 = tmp;
}
if (x.n2.CompareTo(x.n3) < 0)
{
tmp = x.n2;
x.n2 = x.n3;
x.n3 = tmp;
}
if (x.n1.CompareTo(x.n2) < 0)
{
tmp = x.n1;
x.n1 = x.n2;
x.n2 = tmp;
}
return x.n1 + x.n2 + x.n3;
}
#region IEqualityComparer<Line> 成员 public bool Equals(Line x, Line y)
{
return MakeSort(ref x) == MakeSort(ref y);
} public int GetHashCode(Line obj)
{
return MakeSort(ref obj).GetHashCode();
} #endregion
}class MyClass
{
static void Main(string[] args)
{
//准备
Line[] list =
{
new Line{ id = 1, n1 = "c1", n2 = "c2", n3 = "c3" },
new Line{ id = 2, n1 = "c2", n2 = "c3", n3 = "c1" },
new Line{ id = 3, n1 = "c3", n2 = "c1", n3 = "c2" },
new Line{ id = 4, n1 = "c4", n2 = "c1", n3 = "c5" },
}; //执行
Line[] listOk = list.Distinct(new Tools()).ToArray(); //验证
foreach (var x in listOk)
{
Console.WriteLine(x.id + " " + x.n1 + " " + x.n2 + " " + x.n3);
}
//1 c3 c2 c1
//4 c5 c4 c1 Console.ReadKey();
}
}