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
- The LINQ expression could not be translated. Eiither rewrite the query in a form that can be translated
- Use LINQ to compare items in a list
- How to group by 2 items of a list into another list
- Requirements for collection class to be used with LINQ
- Simple LINQ query
- EntityFramework Multiple NavigationProperties to the same table
- Linq - Top value from each group
- how do i refactor these linq properties?
- How to user LINQ to convert list to dictionary?
- .Net Core 5 Rest Api Entity Framework Linq Expression Translation Error While Getting Record List From Controller
- Error with Select statement in Lambda expression
- Linq query - reading nested XML to dictionary
- Reduce columns selected by EF Core
- Which LINQ statements force Entity Framework to return from the DB?
- Linq query with c#
- Getting 'Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken' when retrieving items from JSON
- MVC dealing with multiple LINQ Queries in a single View
- Convert text file to List<int>
- Linq-to-SQL - how to concatenate two fields in a sub select
- Returning the max record count by grouping in linq
- How to get a list of filtered values in Dictionary into a List using LINQ or Lambda?
- string format error when reading CSVs to object with LINQ
- Dictionary with 2 values
- LINQ: Finding an item in a List which contains List of items
- How do I iterate through a string.Split() array, joining LINQ query results for each item together?
- Search in part of the list by LINQ
- Linq: Difference between 2 DateTimes in TimeSpan
- Given collection of strings, count number of times each word appears in List<T>
- Method to get a summary view from a dataset
- Get top n rows and sum the rest and call it others in Entity Framework linq lambda query