Chinajiyong进来一下。。。。。。

zsm5354 2012-03-29 08:11:56
string[] temp = File.ReadAllLines(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
temp = temp.ToList().Distinct().ToArray();
File.WriteAllLines(@"C:\1.txt", temp);

例如数据是这样:
-------------------------------------------
00 2012.05 xxx 12345
00 2012.05 xxx 12300
00 2012.05 xxx 12388
00 2012.06 xxx 12345
我只想判断12345这一列数据是否重复,请问代码该怎么变化呢?
...全文
100 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
string[] temp = File.ReadAllLines(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
temp = temp.ToList().Distinct().ToArray();
File.WriteAllLines(@"C:\1.txt", temp);

例如数据是这样:
-------------------……
[/Quote]
不好意思,来晚了。高手们都解决了,我也贴上我的

string[] temp = File.ReadAllLines(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
List<string> liststr = new List<string>();
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (string tempeach in temp)
{
liststr.Add(tempeach.Split(' ').ToList().Last());
if (!dic.Keys.Contains(liststr.Last()))
{
dic.Add(liststr.Last(), tempeach);
}
}
if (liststr.GroupBy(g => g).ToList().Count < liststr.Count)
{
MessageBox.Show("存在重复");
}
else
{
MessageBox.Show("不存在重复");
}
File.WriteAllLines(@"C:\1.txt", dic.Values.ToArray());//去重复写入文本,默认区第一条
暖枫无敌 2012-03-29
  • 打赏
  • 举报
回复

//定义四个数组
ArrayList number = new ArrayList();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string Path = Server.MapPath("info.txt");
getStr(Path);

for (int i = 0; i < number.Count; i++)
{
Response.Write(number[i].ToString()+" | ");
//这里将所有的最后一列数据取出来了,然后判断是否有重复应该很简单了

}
}
}

//读取txt行中的每一个数据
public void getStr(string Path)
{
string strLine = "";
int i = 0;
try
{
StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"));
while ((strLine=sr.ReadLine()) != null)
{
string[] val = strLine.Split(' ');
//将数据保存在ArrayList集合中
number.Add(val[3]);
Response.Write("当前第"+i+"行:<br/>");
foreach (string s in val)
{
Response.Write(s + "<br>");
}
i++;
Response.Write("<hr>");
}
sr.Dispose();
sr.Close();
}
catch
{
}
}
threenewbee 2012-03-29
  • 打赏
  • 举报
回复
当然你仍然可以使用Distinct(),但是需要传递一个IEqualityComparer:

class MyComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return x.Split(' ')[3] == y.Split(' ')[3];
}

public int GetHashCode(string x)
{
return x.Split(' ')[3];
}
}


string[] temp = File.ReadAllLines(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
temp = temp.ToList().Distinct(new MyComparer()).ToArray();

但是很明显,这种写法多定义一个类。所以我不喜欢。
zsm5354 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
逐行遍历比较最后一列,不就行了
[/Quote]
这个我想过,可就是写不出来
暖枫无敌 2012-03-29
  • 打赏
  • 举报
回复
逐行遍历比较最后一列,不就行了
dupeng20120327 2012-03-29
  • 打赏
  • 举报
回复
那你把数据获取到进行比较不就行了
threenewbee 2012-03-29
  • 打赏
  • 举报
回复
string[] query = (from z in (from x in temp
select new { x, y = x.Split(' ')[3] })
group z by z.y into g
select z.First().x).ToArray();

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧