Extend LINQ syntax
See original GitHub issueI can’t count the number of times I’ve written something like this:
var x = (from item in collection
where item.foo
select item.bar).Single()
Readability takes a nose dive, especially with complex or nested queries. There’s a lot we can do to improve what is one of C#'s most powerful features. From low-hanging fruit that are compatible with existing infrastructure like ORMs:
from item in collection
select single item.foo;
from item in collection
select first item.foo or default;
from item in collection
select sum of item.foo;
from item in collection
skip 5
select top 3 item.foo;
from item in collection
left join item2 in collection2 on item.foo equals item2.foo
select new { item, item2 };
To bits which are currently only usable by LINQ to Objects:
from item in collection
group item.foo by item.bar into grp using StringComparer.InvariantCulture
select grp;
from item in collection by idx
select "item " + item + " at index " + idx;
Issue Analytics
- State:
- Created 9 years ago
- Reactions:6
- Comments:83 (44 by maintainers)
Top Results From Across the Web
Extension methods syntax vs query syntax [duplicate]
I prefer the extension method syntax when I use Linq methods that have no query syntax equivalent, such as FirstOrDefault() or others like...
Read more >Linq Extension Methods in C# with Examples
The LINQ's standard query operators such as select, where, etc. are implemented in the Enumerable class. These methods are implemented as extension methods...
Read more >LINQ Extension Methods in C#: What you need to know
LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to write queries against collections and data sources using ...
Read more >LINQ Method Syntax
Method syntax (also known as fluent syntax) uses extension methods included in the Enumerable or Queryable static class, similar to how you would...
Read more >Giving Clarity to LINQ Queries by Extending Expressions
LINQ expressions can be made much easier to comprehend and modify by using extension methods to create pipes and filters. Ed takes a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We’ll keep this on the backlog as a reminder to consider something here.
The best proposal I’ve seen was
(Or some other keyword). The idea is to add a query operator that is like a
.
but with different precedence.Would like to see if it can handle something like
ToDictionary
,i.e. the range variable is available in the
do
clause.