score:5
That line is selecting all instances n in nameList where the string n contains the string name. So your result will be any of the strings in nameList that have the string name in it.
Also, it is important to break it up into the two parts. First, this is a Linq query. You could do this to find all the items in nameList that equal name exactly: var filteredStringList = from n in nameList where n == name select n;
Your where statement "n.IndexOf(name, 0, StringComparison.OrdinalIgnoreCase) != -1" just changes the simpler query, "n == name" to filter in a slightly different way. First, the n.IndexOf(name) method gets the first starting index where the string name occurs in n. Any value >= 0 means that name exists in the string. -1 is returned if the string doesnt exist. The other arguments are the index where to start the search, in your case 0, and the string comparison, in your case StringComparison.OrdinalIgnoreCase. StringComparison.OrdinalIgnoreCase tells the string comparison to treat A and a as the same and so on.
Edit: @Jason had a good point to consider also. While strictly speaking the query is not actually doing the iteration, it is actually creating a linq expression. The expression will execute only after you call something like filteredStringList.ToList() or a similar call. For all intents and purposes, the result is the same, but it is important to know when the query will actually execute. See this post for more details: http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx?wa=wsignin1.0
score:-1
It's saying if a variable called name exists in nameList then select it into filteredStringList.
score:0
"get all entries of nameList which contain name"
score:-2
Your LINQ can be interpreted as
for (int i = 0; i < nameList.Count; i++)
{
if (nameList[i].IndexOf(name, 0, StringComparison.OrdinalIgnoreCase) != -1)
{
TempList.Add(nameList[i]);
}
}
here TempList
is List<String> TempList = new List<string>();
In LAMBDA Expression you can write this as
var filteredStringList = nameList.Where(X => X.IndexOf(name, 0, StringComparison.OrdinalIgnoreCase) != -1);
score:1
var filteredStringList =
from n in nameList
where n.IndexOf(name, 0, StringComparison.OrdinalIgnoreCase) != -1
select n;
This is LINQ (specifically the query syntax form), and exactly what is happening is a little complicated and subtle.
The rough idea is that this block of code creates an iterator. When this iterator is iterated over, it will filter nameList
by only selecting the elements of nameList
that satisfy the predicate p(n) = n.IndexOf(name, 0, StringComparison.OrdinalIgnoreCase) != -1
. That is, it will only select the elements of nameList
that contain name
(ignoring case).
It is very important that you understand that filteredStringList
is not a list (thus, it is horribly named). It does not contain the results of the filtering. It only creates an object that captures the rules for building the filtered subsequence of nameList
when it is iterated over.
Source: stackoverflow.com
Related Articles
- what does this .net line of code means
- What does this C# code with an "arrow" mean and how is it called?
- What does this expression means in the GroupBy method?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Does this LINQ code perform multiple lookups on the original data?
- What does Any() mean in this LINQ query?
- Does this code really cause an "access to modified closure" problem?
- How does this linq code that splits a sequence work?
- OrderByDescending() per MSDN, what on earth does this mean?
- What does this LINQ query do?
- What does this mean in C# or LINQ? - ( () => )
- What does the code query.Take(() => 1) do?
- What would be a reasonably fast way to code this sql query in c#?
- why does this linq code get exponentially slower when applying First() to projection?
- Task Does Not Contain a Definition for Where If Done in One Line of Code
- Reduce the line of code for this LINQ query
- What is logically wrong about this VB.NET code
- Code disentangle - what does query of T do
- C# XML - Why does this Code keep failing with 0x3A Error?
- What does this LINQ query do in C#?
- What is this linq code doing? Summarize something?
- What would be the Linq code for this grouping using VB.NET 2008?
- Why this multi linq join code does not work?
- What does LINQ return when the results are empty
- Convert string[] to int[] in one line of code using LINQ
- What is LINQ and what does it do?
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- What does Include() do in LINQ?
- Why does the C# compiler go mad on this nested LINQ query?
- Why does this method result in an infinite loop?
- What's the best way to compile a list of all Parent objects of a list of child objects?
- How to reduce this IF-Else ladder in c#
- LINQ query through Azure blobs of IEnumerable(Of IListBlobItem)
- C#/LINQ Automated filtering on all tables
- Workaround for MethodBase.GetCurrentMethod() on Compact Framework 3.5
- Include with additional param on join
- Get data from a table after joining based on null value of joined table using LINQ
- Why does the Contains() operator degrade Entity Framework' Linq queries?
- SelectMany from grouped element
- LINQ LEFT JOIN not working on NULL values
- The LINQ expression could not be translated about sum of Property TimeSpan
- It makes no sense why "Specified cast is not valid." in this case. Or does it?
- Having some trouble understanding Linq's INTO keyword
- What is the way to join these two list?
- Possible uses for Intersect
- Using linq to join/union two objects with different columns
- Entity Framework: Linq !contains based on table from different database
- c# Linq select join on select group by
- How can I add an IEnumerable<T> to an existing ICollection<T>
- EF: OrderBy is not working when I define a delegate with the same signature