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.
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
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.
- Why does IEnumerable's Where pass first argument as "this"
- What does this C# code with an "arrow" mean and how is it called?
- Why does this combination of Select, Where and GroupBy cause an exception?
- Does this LINQ code perform multiple lookups on the original data?
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- Does this code really cause an "access to modified closure" problem?
- How does this linq code that splits a sequence work?
- Linq with where clause in many-to-many EF Code First object
- 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>' could be found
- Where does "i" get its value in this LINQ statement?
- Where does this LINQ performance come from?
- How to query by where clause with EF code first
- Where does this Linq statement get the selected value from?
- why does this linq code get exponentially slower when applying First() to projection?
- CS1061: 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>'
- what does this .net line of code means
- int' does not contain a definition for 'contains' and no extension method 'contains' accepting a first argument of type 'int' could be found
- Code does not seem to pass a certain line, causing StackOverflowException
- Task Does Not Contain a Definition for Where If Done in One Line of Code
- 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?
- List does not contain a definition for 'ConvertAll' and no extension method 'ConvertAll' accepting a first argument of type 'List< >'
- EF GroupBy: does not contain a definition for 'BriefTitle' and no extension method 'BriefTitle' accepting a first argument of type
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- C# XML - Why does this Code keep failing with 0x3A Error?
- MVC 5 ASP.NET IEnumerable<Item> does not contain a definition for "Inspection" and no extension method "inspection" accepting a first argument of type
- Why this multi linq join code does not work?
- 'LINQ.SalesTaxRate' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Why does the C# compiler go mad on this nested LINQ query?
- Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
- Linq Statement when you need to do a Convert.To?
- Converting VB Linq to C#
- LINQ Equivalent MYSQL Query
- Displaying records for two weeks only! from creation
- A better way than generic list of anonymous type
- How to Read This Text File and store in a list using C#
- AutoMapper throws exception when projecting to nullable enum
- DateTime just the date?
- Entity Framework Shared Core Functionality Library
- C# lambda IN clause error
- Can a method chain be called LINQ?
- Using Linq to populate a class
- GroupBy peculiar behaviour
- LINQ statement value to ForEach C# WPF MVVM
- Get XElement from XML using LINQ
- Entity Framework: Selecting distinct objects
- Entity framework navigation property is null
- Linq Sort IEnumerable on multiple tiers
- query nested objects with LINQ
- How to get intersection in Entity Fraemwork if relationship between tables many-to-many?