score:6
that particular overload of the where
extension method is defined to accept a func
where the first parameter is the element and the second parameter is the index of that element. this is mentioned explicitly in the documentation:
predicate
type:
system.func<tsource, int32, boolean>
a function to test each source element for a condition; the second parameter of the function represents the index of the source element.
emphasis mine.
score:0
because there is an overload of where
taking as a predicate a function like this:
func<tsource, int, bool>
the semantic of this predicate ios defined so the second parameter is the index. so if you pass a lambda with two args that version of where is called.
score:1
the overload of where
that you are using is implemented like this:
public static ienumerable<t> where<t>(
this ienumerable<t> list, func<t,int,bool> predicate)
{
int num = -1;
foreach (t current in list)
{
num++;
if (predicate(current, num))
{
yield return current;
}
}
yield break;
}
as you can see, on each iteration, the predicate (i.e. the passed lambda expression) is invoked passing the current element of the list and the current index.
in this way, the code in the lambda expression know both the element and its index.
score:2
this is just a naming thing, you could name digit anything, and index anything. this is just a lambda expression for an anonymous function...it could be rewritten:
(string digit, int index) => digit.length < index
so, the names are the same as any parameter you normally set up in a method. there is nothing truly special about the names being recognized by the c# engine.
(string index, int digit) => index.length < digit
the above would work also...it would be confusing, but it would work. it is just to show that the name can be whatever you want
if you are referring how to the signature itself then it is due to an overload of the where function
public static ienumerable<tsource> where<tsource>(
this ienumerable<tsource> source,
func<tsource, int, bool> predicate
)
so, the tsource in this case is string
, making the func become func<string, int, bool>
. meaning that the lambda must take a string
parameter followed by an int
param, and return a bool
value
Source: stackoverflow.com
Related Query
- Trying to understand LINQ code using c#
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- Convert string[] to int[] in one line of code using LINQ
- Trying to get all elements after first match using linq
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- foreach and linq query - need help trying to understand please
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- LINQ Source Code Available
- Trying to get a list of files in a directory by created date using LINQ
- How can I write the following code more elegantly using LINQ query syntax?
- Problems trying to use GroupBy with multiple properties using the LINQ Method Syntax
- How can I code an outer join using LINQ and EF6?
- I am trying create a new list of users from a list of users who have not created an additional object with a certain property using LINQ
- C# .Net 3.5 Code to replace a file extension using LINQ
- Retrieve bool result by using LinQ code
- Trying to pivot data using linq
- Trying to get distinct values using LINQ with database field as parameter
- creating Linq to sqlite dbml from DbLinq source code
- read icollection data using LINQ in C# code
- Linq sub query when using a repository pattern with EF code first
- Using LINQ query result for data source for GridControl c#
- How to flatten a multi level XML into a single level XML using c# code LINQ
- Trying to create a "Fluent" type class using LINQ expressions
- Using Linq to build a graph class; can you make this code look better?
- How to write this code using the Linq Extension Method-Syntax?
- Code Rewite for tuple and if else statements by using LINQ
More Query from same tag
- How do I find duplicates in XML if an element contains value in C#?
- LINQ C# Creating Dictionary of Dictionaries using GroupBy
- LINQ - list group by
- How can I query a dataset based on double columns being "close to" some value?
- Differences between IEquatable<T>, IEqualityComparer<T>, and overriding .Equals() when using LINQ on a custom object collection?
- Regarding LINQ Usage in Large Loops
- How to iterate a tree structure with logic on where to stop
- Can't call the Elements() method for System.Xml.Linq in Webmatrix site
- How to selected and item from Entity and insert the item in other table having same structure using c#?
- ElementAt(index) on ICollection<T>
- Implement Linq query on byte[] for my own type
- Passing Aggregation to a Linq Query
- In my MVC app, why is the performance so slow after adding this code?
- Why is this LINQ query incredibly slow?
- LINQ group by and getting latest value
- Expose Foreign key through fluent API
- Determining free time between calendar entries
- Paging API calls using Linq to Sql OrderBy with string parameter and reflection
- Compare two Lists by specific properties
- Linq Query Join to Subquery with In clause
- How to dynamically instantiate 3 different classes into the same variable name, linq2sql c#
- how to fix SqlError :failed when converting date and/or time from character string. EF binaryexpression?
- TextBox not display value after ComboBox onChange (WPF, C#, Binding)
- C# Method to Compare DateTime Fields Specified During Execution?
- How to write an expression for string concatenation for linq?
- Error: LINQ to Entities does not recognize the method
- How to replace large count of string values from list found in the string
- Join two dictionaries using a common key
- LINQ, simplifying expression - take while sum of taken does not exceed given value
- How to get the re-occurrence count of an item in an IList? (Without Immutable in loop)