Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a LINQ syntax for the (T, int) overloads of Where and Select?

The query

var q = from elem in collection
        where someCondition(elem)
        select elem;

translates to

var q = collection.Where(elem => someCondition(elem));

Is there a LINQ syntax that would translate to the following?

var q = collection.Where((elem, index) => someCondition(elem, index));
like image 408
Timwi Avatar asked Sep 21 '10 06:09

Timwi


People also ask

How to use Where clause in LINQ?

The where clause is used in a query expression to specify which elements from the data source will be returned in the query expression. It applies a Boolean condition (predicate) to each source element (referenced by the range variable) and returns those for which the specified condition is true.

Which Linq method allows you to specify the number of items to return in a sequence?

Repeat Method is used to generate a collection of IEnumerable<T> type with a specified number of elements and each element contains same specified value. Second Paramter "int count" represents-> Number of times needed to repeat the generated element of sequnce.

What is select new Linq?

@CYB: select new is used when you want your query to create new instances of a certain class, instead of simply taking source items. It allows you to create instances of a completely different class, or even an anonymous class like in OP's case.

What is take function C#?

C# Queryable Take() MethodGet specified number of elements from the beginning using the Take() method. The following is our array. int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 }; Now, use OrderByDescending to order the elements in Descending order. Then use the Take() method to get the elements.


1 Answers

No there's no LINQ syntax for that.

A simple work-around could be:

var q = from elem in collection.Select((x,i) => new {x,i})
        where someCondition(elem.x,elem.i)
        select elem.x;
like image 171
digEmAll Avatar answered Oct 06 '22 00:10

digEmAll