我是Linq的新手。我有一个Customers表,其中ID、FullName、Organization和Location是列名。我在Sqlite中有一个查询,返回2500条客户记录。我需要从这个结果集中找到ID=150的客户的索引。这是一个客户列表,查询结果按组织排序。我尝试使用FindIndex和IndexOf方法,但前者出现错误,后者返回-1。请问应该如何解决?谢谢。
您不需要使用 LINQ
,您可以使用 List<T>
的 FindIndex
方法:
int index = customers.FindIndex(c => c.ID == 150);
Linq to Objects 重载了 Select 方法。
customers.Select((c,i) => new { Customer = c, Index = i })
.Where(x => x.Customer.ID == 150)
.Select(x => x.Index);
请记住,您需要在内存中拥有 List<Customer>
才能使用此 Linq to Objects 方法。
List<T>
而不是任何IList<T>
时才有用。 - O. R. MapperArray.FindIndex<T>
。 - Tim SchmelterToArray()
获得的数组的情况下,永远没有使用ToList()
的好理由。 - O. R. Mapper