List 移除数据

苦逼的程序员 2011-05-10 11:50:20
若List<User> user 里有若干数据是
username pwd type
aa aa 1
bb bb 2
aca asaa 3
sbb bdb 1
vaa aa 1
bbc bbb 3
aca aba 2
bnb bnb 2
怎么循环遍历出Type为2的所有数据 user2
...全文
337 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
小童 2011-05-10
  • 打赏
  • 举报
回复
List<User> user2=user.Where(u=>u.type==2).ToList();


List自身有一个Find方法,要求传一个委托,自己看看
骑猪看海 2011-05-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]
List<User> user2=user.Where(u=>u.type==2).ToList();
[/Quote]
+2
我姓区不姓区 2011-05-10
  • 打赏
  • 举报
回复
List<User> user2 = user.FindAll(u=>u.Type==2);
chen_ya_ping 2011-05-10
  • 打赏
  • 举报
回复
for
foreach
linq-where,都是可以的啊。
q107770540 2011-05-10
  • 打赏
  • 举报
回复
List<User> user2=user.Where(u=>u.type==2).ToList();
zell419 2011-05-10
  • 打赏
  • 举报
回复
List<User> user2 = new List<User>();
for(int i = 0 ; i <user.count ;i++)
{
if(user[i].Type==2) user2.add()user[i];
}
davidcoffee 2011-05-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hualilihua 的回复:]
引用 11 楼 davidcoffee 的回复:
用LINQ最方便弄了个演示程序,体现了LINQ的优势了~

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class Program
{
stat……
[/Quote]那你别用别人封装的代码你都自己实现好了,太2B了~
beckfun 2011-05-10
  • 打赏
  • 举报
回复
又或者还可以这样写..呵呵...




user.RemoveAll(new Predicate<User>(delegate(User u) { return u.type != 2; }));

删除不相干的..剩下的就都是你想要的那些了..
beckfun 2011-05-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 beckfun 的回复:]

已经有那么多回答了...我就再献丑写个2.0版本的吧...
C# code

List<User> usersLoist = user.FindAll(new Predicate<string>(delegate(User u) { return 2 == u.type; }));
[/Quote]
呃..点的太快..写错一个地方..正确的应该是这样的


List<User> usersLoist = user.FindAll(new Predicate<User>(delegate(User u) { return 2 == u.type; }));
beckfun 2011-05-10
  • 打赏
  • 举报
回复
已经有那么多回答了...我就再献丑写个2.0版本的吧...

List<User> usersLoist = user.FindAll(new Predicate<string>(delegate(User u) { return 2 == u.type; }));
回头是岸 2011-05-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 davidcoffee 的回复:]
用LINQ最方便弄了个演示程序,体现了LINQ的优势了~

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class Program
{
static void Ma……
[/Quote]

优势个毛 只不过是别人封装了循环罢了
davidcoffee 2011-05-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zmcici 的回复:]
引用 2 楼 q107770540 的回复:
List<User> user2=user.Where(u=>u.type==2).ToList();

+2
[/Quote]
也很简洁明了,学习了~顺便友情帮顶了~
davidcoffee 2011-05-10
  • 打赏
  • 举报
回复
用LINQ最方便弄了个演示程序,体现了LINQ的优势了~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class Program
{
static void Main(string[] args)
{
List<User> testList = new List<User>();
User aa = new User("aa", "bb", 1);
User bb = new User("bb", "bb", 2);
User ass = new User("ass", "ass", 3);
User sbb = new User("sbb", "sbb", 1);
User vaa = new User("vaa", "vaa", 1);
User bbc = new User("bbc", "bbc", 3);
User aca = new User("aca", "aca", 2);
User bnb = new User("bnb", "bnb", 2);
testList.Add(aa);
testList.Add(bb);
testList.Add(ass);
testList.Add(sbb);
testList.Add(vaa);
testList.Add(bbc);
testList.Add(aca);
testList.Add(bnb);
var result = from target in testList
where target.Type == 2
select target;
foreach (User item in result)
{
Console.Write("name:{0}, password:{1}, type:{2}", item.Username, item.Pwd, item.Type);
Console.WriteLine();
}
Console.WriteLine("共有{0}条记录", result.Count());
Console.ReadLine();
}
}

public class User
{
private string username;

public string Username
{
get { return username; }
set { username = value; }
}
private string pwd;

public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
private int type;

public int Type
{
get { return type; }
set { type = value; }
}

public User() { }

public User(string username, string pwd, int type)
{
this.username = username;
this.pwd = pwd;
this.type = type;
}
}
}


donotsmoking 2011-05-10
  • 打赏
  • 举报
回复
list.Where(str => str.Length > 0);
到3。0及以后都用上面的方法就可以了。
fire_fire_fire 2011-05-10
  • 打赏
  • 举报
回复
这个是正解
List<User> user2 = new List<User>();
for(int i = 0 ; i <user.count ;i++)
{
if(user[i].Type==2) user2.add(user[i]);
}
你不能在循环中直接移除user集合的数据,因为你移除后循环的条件跟着就变了,可能会遇到异常
应该把符合条件的数据添加到另外一个集合中
Adechen 2011-05-10
  • 打赏
  • 举报
回复
linq的比较的简单,

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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