我在List里面存了很多值,List是按下面的结构放入的
id name sex ,里面存放的情况如下1 222 1
2 333 1
3 555 2
4 888 1
5 333 2
现在我需要吧List里面name为222的取出来 也就是吧“1 222 1”者几个值都取出来怎么做啊,现在急求~
id name sex ,里面存放的情况如下1 222 1
2 333 1
3 555 2
4 888 1
5 333 2
现在我需要吧List里面name为222的取出来 也就是吧“1 222 1”者几个值都取出来怎么做啊,现在急求~
List<string> list = new List<string>();
list.Add("1 222 1");
...
var d = list.Find(p => p.Split(' ')[1] == "222");
Response.Write(d.ToString());
List<Temp> sss = new List<Temp>();
sss.Find(x => x.Name = "222");
var d = list.Find(p => p.name == "222");
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{
public struct Student
{
public string Id;
public string Name;
public string Sex;
} class Program
{ static void Main(string[] args)
{
List<Student> lstStudent = new List<Student>();
//添加数据
lstStudent.Add(new Student
{
Id = "1",
Name = "1111",
Sex = "1"
});
lstStudent.Add(new Student
{
Id = "2",
Name = "2222",
Sex = "2"
});
//得到数据方法1:Linq
var student = lstStudent.AsEnumerable().Where(x => x.Name == "2222").SingleOrDefault();
Console.WriteLine("Id:" + student.Id);
Console.WriteLine("Name:" + student.Name);
Console.WriteLine("Sex:" + student.Sex);
//得到数据方法2:循环遍历
foreach (var s in lstStudent)
{
if (s.Name == "2222")
{
Console.WriteLine("Id:" + s.Id);
Console.WriteLine("Name:" + s.Name);
Console.WriteLine("Sex:" + s.Sex);
}
} Console.ReadLine();
}
}
}输出结果为:
Id:2
Name:2222
Sex:2
Id:2
Name:2222
Sex:2
楼主说的是List,不是Dictionary<key,value>,从哪来的关键字。
应该用索引。
list[0]