问题
I have a IQueryable which is ordered by some condition. Now I want to know the position of a particular element in that IQueryable. Is there a linq expression to get that. Say for example there are 10 elements in the IQueryable and the 6th element matches a condition, I want to get the number 6.
回答1:
First select each item with its index, then filter the items, and finally extract the original index:
var result = orderedList
.Select((x, i) => new { Item = x, Index = i })
.Where(itemWithIndex => itemWithIndex.Item.StartsWith("g"))
.FirstOrDefault();
int index= -1;
if (result != null)
index = result.Index;
Test bed:
class Program
{
static void Main(string[] args)
{
var orderedList = new List<string>
{
"foo", "bar", "baz", "qux", "quux",
"corge", "grault", "garply", "waldo",
"fred", "plugh", "xyzzy", "thud"
}.OrderBy(x => x);
// bar, baz, corge, foo, fred, garply, grault,
// plugh, quux, qux, thud, waldo, xyzzy
// Find the index of the first element beginning with 'g'.
var result = orderedList
.Select((x, i) => new { Item = x, Index = i })
.Where(itemWithIndex => itemWithIndex.Item.StartsWith("g"))
.FirstOrDefault();
int index= -1;
if (result != null)
index = result.Index;
Console.WriteLine("Index: " + index);
}
}
Output:
Index: 5
回答2:
You could use something like query.TakeWhile(x => !matchesCondition(x)).Count()
, though that has the effect of enumerating the preceding values, which may not be what you want.
回答3:
You can also use the verson of the "Where" function that incldes the collection index as a parameter to the predicate function. See MSDN for more info.
var result = Enumerable.Range(0, 10).Where((x, i) => i == 6);
The version could result in an empty list if there isn't a 6th element. Also this doesn't evaluate the where clause until you iterate over the result.
来源:https://stackoverflow.com/questions/1869304/determine-the-position-of-an-element-in-an-iqueryable