Because it is an Extension Method.
So that instead of
IEnumerable<string> filteredNames = System.Linq.Enumerable.Where(names, n => n.Length >= 4);
You can also use it like:
IEnumerable<string> filteredNames = names.Where(n => n.Length >= 4);
The Reason it is an extension method is that IEnumerable, List, ... existed long before Linq (which was introduced in .Net 3.5) and its job is just to extend finding, filtering, ordering, ... them. So it is logical to have it as an Extention method rather than a separate library. And also consider that this way you can use chaining, which woudln'd be possible if it wasn't an extension:
name.Where(x => x.Length > 4).Select(x => x.Substring(4));
Compare it to:
System.Linq.Enumerable.Select(System.Linq.Enumerable.Where(name, x => x.Length > 4), x => x.Substring(4));
And this is only a very simple one, consider how dirty it gets with larger, complex queries.
First of all, The simple answer is because it is an extension method. The definition of extension method is that Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Secondly, I disagree Ashkan Mobayen Khiabani's answers last part. you must have a look fluent implementation without extension method.
Since it's an Extension Method. It means that Where is not a method on IEnumerable but when you reference Linq namespace ,Where method is added to IEnumerable.
for more info read this : Extension Methods
- Why does IEnumerable's Where pass first argument as "this"
- Why does this combination of Select, Where and GroupBy cause an exception?
- why does this linq code get exponentially slower when applying First() to projection?
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- C# XML - Why does this Code keep failing with 0x3A Error?
- Why this multi linq join code does not work?
- Why does the C# compiler go mad on this nested LINQ query?
- Why does this method result in an infinite loop?
- What does this C# code with an "arrow" mean and how is it called?
- Why does this Linq Cast Fail when using ToList?
- Why does adding an unnecessary ToList() drastically speed this LINQ query up?
- why is this linq query return a boolean and not the first result of the select?
- Why does EF 5.0 not support this EF 4.x LINQ syntax when compiling to sql?
- Why does the order of LET statements matter in this Entity Framework query?
- Why does this error occur when using SingleAsync?
- Does this LINQ code perform multiple lookups on the original data?
- Why doesn't this code compile in VS2010 with .NET 4.0?
- LINQ: why does this query not work on an ArrayList?
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?
- Why does this LINQ grouping have a count of 3 instead of 2?
- Why does LINQ-to-Entities put this query in a sub-select?
- Why does this LINQ-to-SQL query get a NotSupportedException?
- Why is this code with PredicateBuilder not working?
- Does this code really cause an "access to modified closure" problem?
- Why does adding a list to another list, using add range, remove the elements from the first list?
- Why does the compiler think this is an Object instead of a DataRow?
- Why does this LINQ query compile?
- How does this linq code that splits a sequence work?
- Why does this CollectionAssert.AreEquivalent() test fail?
More Query from same tag
- What does the code query.Take(() => 1) do?
- Linq statement using where in
- Linq to XML - Extract Single Element
- Error: Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context
- Custom (derived) List<T>
- ToListAsync() in a DbContext using statement : "The ObjectContext disposed", How to deal?
- How to use MongoDB C# Driver Aggregate to get the latest item with specific values on 2 fields?
- Convert ParameterInfo to RouteValueDictionary via LINQ
- Sorting List of Table objects by their Relations
- SQL Query inner join in Linq
- Condition merge while merging duplicate of two List
- How to add ListView Items via Binding by Entity Framework / Linq
- C# Replace all elements of List<string> with the same pattern with LINQ
- How extension methods work in background?
- Deleting rows in a table with Linq to SQL
- Any way to access to a variable of type "var", defined in code behind, from aspx file?
- How to get the children of a string according to the dot
- Advanced LINQ Grouping and Projection Query for Hierarchical Data (EF 4.0 + LINQ + ASP.NET MVC + HighCharts)
- Composing invocations with Expression<Func<T,bool>> the same way as Func<T,bool>
- NHibernate Linq - how to select WHERE IN
- c# Writing an extension method to extend functionality in System.Linq
- how to group by name having different Ids in linq
- Simple LINQ query to count by month
- Unable to set enum property during LINQ select
- Can someone help me to create a LINQ to XML query?
- SQL to entities - complex query selecting dates
- LINQ query to get data from a table connected by three tables away
- coalescing in linq join for equals
- grouping and Update Property in linq
- Parsing a XDocument