使用Linq获取列表中的对象的索引 [英] Get index of object in a list using Linq

查看:149
本文介绍了使用Linq获取列表中的对象的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linq的新手。我有一个Customers表.ID,FullName,组织,位置是列。我有一个查询在Sqlite返回我的客户2500记录。例如,我必须从这个结果集中找到ID = 150的客户索引。它的客户名单。查询的结果集按组织排序。我尝试使用FindIndex和IndexOf,但获取前者的错误和-1后者。那么,该怎么办呢?
Thanks。

解决方案

您不需要使用 LINQ ,可以使用 FindIndex $ b $ pre $ code $> code>列表< T>

int index = customers.FindIndex(c => c.ID == 150);


I am new to Linq. I have a Customers table.ID,FullName,Organization,Location being the columns. I have a query in Sqlite returning me 2500 records of customers. I have to find the index of the customer where ID=150 for example from this result set. Its a List of Customers. The result set of the query is ordered by organization. I tried with FindIndex and IndexOf but getting errors for the former and -1 for the latter. So, how should it be done? Thanks.

解决方案

You don't need to use LINQ, you can use FindIndex of List<T>:

int index = customers.FindIndex(c => c.ID == 150);

这篇关于使用Linq获取列表中的对象的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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