Why is LINQ allowed to have spaces in the statement? An example statement:
var questions = from item in db.questions
select item;
As a programmer, we cannot create functions or methods with spaces in them, or anything that resembles the LINQ syntax. Is this something that is just specially parsed by the language? Would there be any way to let programmers make up their own LINQ-like statements?
Because they are “keywords” (technically they are “contextual keywords”. They are “keywords” only in certain places) 🙂 Look here: Query Keywords (C# Reference) and C# Keywords
Why can you write
public static voidwith spaces? Because they are keywords 🙂And no, you can’t add new keywords to C#.
(but note that you can use the LINQ syntax on non-IEnumerable/IQueryable objects. LINQ syntax is converted “blindly” to specific method names. The compiler doesn’t check if they are really
IEnumerable<T>orIQueryable<T>)Try this: