Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C# Feature] Support more feature for LINQ query #2970

Closed
rexxiang opened this issue May 21, 2015 · 1 comment
Closed

[C# Feature] Support more feature for LINQ query #2970

rexxiang opened this issue May 21, 2015 · 1 comment

Comments

@rexxiang
Copy link

In some complex situation, the LINQ query will be loss the readability and hard to maintain.
so it would be better if the LINQ query syntax improved like below.

allow more condition expression in the join clause. the equals expression is the only choice nowdays.

from l in EntityList
join r in EntityList on l.ParentId == r.ParentId && l.Id > r.Id
select r

allow left join clause in LINQ to let the query more simplify .

from l in EntityList
left join r in EntityList on l.ParentId == r.ParentId && l.Id > r.Id
select r

I'm not good at English, but hope you guys understand what I'm saying. 😄

@aluanhaddad
Copy link

For the first example proposed:
In terms of LINQ to Objects the current semantics of the join clause are that it does not enumerate the right sequence for each item in the left sequence. Instead it produces the keys used in the equals clause a single time for each sequence and correlates items accordingly.
This has two nice properties:

  1. It improves performance when items only need to be correlated thusly.
  2. It may improve readability in these cases by highlighting the simplicity of the correlation. This can be especially true for scenarios which perform ad hoc correlations.

Continuing with LINQ to Objects, the first example proposed:

from l in EntityList
join r in EntityList on l.ParentId == r.ParentId && l.Id > r.Id
select r

Would currently quite clearly written as either:

from l in EntityList
from r in EntityList 
where l.ParentId == r.ParentId && l.Id > r.Id
select r

or, if we want potentially better performance*:

from l in EntityList
join r in EntityList on l.ParentId equals r.ParentId 
where l.Id > r.Id
select r

Either option is very readable in my opinion.

*note that equals and == can behave differently for some types

My reason for the above statements is to clarify the nature of the first part of the feature request.
Is it because that you find join on x == y && a > b more readable than the currently available options?

The reason I am specifying Linq to Objects is that an external query provider, such as LINQ to SQL is free to optimize the multiple from clauses into a compound join if it so chooses.

In the second example:

from l in EntityList
left join r in EntityList on l.ParentId == r.ParentId && l.Id > r.Id
select r

I think this could be useful as it could clean up or clarify some code that currently look something like this:

entityList.SelectMany(l => EntityList
    .Where(r => l.ParentId == r.ParentId && l.Id > r.Id)
    .DefaultIfEmpty())

Although I think it would be better to add query expression support for more generalized cases as is proposed in #100 Extend LINQ syntax.
Update: Actually left join syntax is also already proposed in #100.

@rexxiang rexxiang closed this as completed Oct 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants