如何在列表中搜索 [英] how to search in list

查看:75
本文介绍了如何在列表中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望列表中的searchig代码..这相当于从列表中查找名称,其中id = 1

解决方案

有很多方法要做到这一点,但最简单的可能是使用Linq方法:

假设:

  public   class  MyClass 
{
public int ID { get ; set ; }
public string 名称{ get ; set ; }
}

然后:

列表< MyClass> list =  new  List< MyClass>(); 
...
var matching = list.Where(i = > i.Id == 1 );


您可以使用 Linq 为此。

例如 -



假设您的人员数据名称集合为 PersonData

 List< string> names =( from  person  in  PersonData  where  person.PersonId ==  1   select  person.PersonName)。ToList(); 





这将返回ID为1的人名列表。

希望这对您有所帮助。


i want code for searchig in the list.. which is equivalent to "find name from list where id= 1"

解决方案

There are a lot of ways to do it, but the easiest is probably to use a Linq Method:
Assuming:

public class MyClass
    {
    public int Id { get; set; }
    public string Name { get; set; }
    }

Then:

List<MyClass> list = new List<MyClass>();
...
var matching = list.Where(i => i.Id == 1);


You can use Linq for this.,
For example -

Suppose you have collection of Person data name as PersonData

List<string> names = (from person in PersonData where person.PersonId == 1 select person.PersonName).ToList(); 



This will return the list of Person Name which id is 1.
Hope this will help you..


这篇关于如何在列表中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆