question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extend LINQ syntax

See original GitHub issue

I 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:closed
  • Created 9 years ago
  • Reactions:6
  • Comments:83 (44 by maintainers)

github_iconTop GitHub Comments

11reactions
MadsTorgersencommented, Aug 15, 2016

We’ll keep this on the backlog as a reminder to consider something here.

The best proposal I’ve seen was

var x = 
    from item in collection
    where item.foo
    select item.bar
    do Single();

(Or some other keyword). The idea is to add a query operator that is like a . but with different precedence.

3reactions
alrzcommented, Aug 17, 2016

Would like to see if it can handle something like ToDictionary,

var x = 
    from item in collection
    where item.foo
    do ToDictionary(item.Key, item.Value);

i.e. the range variable is available in the do clause.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found