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));
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.
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.
@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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With