当前位置:编程学习 > C#/ASP.NET >>

|zyciis| 我的一个DataTable有10000行,现在要对按一定条件进行筛选 哪种方法最科学

如我的DataTable
Type  Name
2     AA
2     CC
3     AA
3     BB
4     BB
4     CC
... ...
-----------------------------
要求是不管Type
当Name有相同 只保留最前一条记录,删除其他记录

你上面为
Type  Name
2     AA
2     CC
3     BB
... ...

我是用嵌套两个for()来完成的
但感觉这样的话10000行就是10000 * 9999 * 9998 * 9997 ... * 2 * 1
他运行的次数太恐怖了吧
有没有更好的办法

谢谢 --------------------编程问答-------------------- Order一下,从1到10000按顺序取下去,只要下一个跟当前一个相同,就跳过,这样复杂度就只有10000了 --------------------编程问答-------------------- 是.NET3.5吗?如果是,直接用LINQ来搞定.如果不是,在数据库上想想办法
select min(type) as type,name from table group by name --------------------编程问答-------------------- 不是3.5 是2.0的
谢谢 --------------------编程问答-------------------- 存储过程。。 --------------------编程问答-------------------- 存储过程第一次慢。。然后就快多了。。它主要就是应用了复杂的,数据量大的数据。。 --------------------编程问答-------------------- RE:大家
----------------------
不好意思,我这个不是从数据库里面读出来的

这是一个DataTable 

谢谢 --------------------编程问答--------------------
引用 6 楼 zyciis729 的回复:
RE:大家 
---------------------- 
不好意思,我这个不是从数据库里面读出来的 

这是一个DataTable  

谢谢


恩?你的DataTable里的数据是从哪来的? --------------------编程问答-------------------- 处理冗余数据问题

http://blog.csdn.net/yuxuanji/archive/2007/12/19/1953568.aspx

string sql=@"delete from 你的表名 where Type  
not in(select Type =min(Type) from 你的表名 group by Name ); select * from 你的表名"

用这个sql作为参数传入你的command或者adapter对象 --------------------编程问答--------------------

---直接用SQL,写法太多:
--1、
select * from 表名 a where not exists(select 1 from 表名 where Name=a.Name and type<a.type)

--2、
select * from 表名 a where type=(select min(type) from 表名 where Name=a.Name)

--3、
select * from 表名 a where (select count(distinct type) from 表名 where Name=a.Name and type<=a.type)=1

--4、
select a.* from 表名 a,(select min(type),name from 表名 group by name)b
where a.name=b.name

--等等```
--------------------编程问答-------------------- 二楼正解 --------------------编程问答-------------------- 如果直接删除

delete 表名 from 表名 a where exists(select 1 from 表名 where Name=a.Name and type<a.type)

--------------------编程问答-------------------- 等 --------------------编程问答--------------------             DataTable dt = new DataTable();
            //...      取数据填入表中
            dt.DefaultView.Sort = "Name";//对表按字段排序
            string temp = "";
            foreach (DataRow row in dt.Rows)
            {
                if (temp == row["Name"].ToString())
                {
                    row.Delete();
                }
                else
                {
                    temp = row["Name"].ToString();
                }
            } --------------------编程问答-------------------- RE: 大家看清楚了啊
------------------------

我不个不是数据库
我这是一个DataTable





谢谢






--------------------------- --------------------编程问答-------------------- 以上代码没有上机试过,请楼主自己修改一下。
主要思路是先对表排序,然后一条条的对比,只要10000次就可以出结果。
如果表是从数据库取,则只用一条SQL命令就可搞定。 --------------------编程问答-------------------- RE:楼上
---------------
不错先排了序的话删除就不用再再里面for一下
不错 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 17楼的太无耻了 生儿子没屁眼 --------------------编程问答-------------------- 有点很不明白 --------------------编程问答-------------------- 首先group by 出来,然后再处理删除
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,