开发者

Determine the position of an element in an IQueryable

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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜