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

Question: why is Distinct not supported? #21

Closed
Vlandmist opened this issue May 30, 2016 · 18 comments
Closed

Question: why is Distinct not supported? #21

Vlandmist opened this issue May 30, 2016 · 18 comments
Assignees
Labels

Comments

@Vlandmist
Copy link

Distinct() method is not supported, not in the original @NArnott project (now disappeared.. why?) and not in this fork. what's the reason behind it? is it too difficult to implement?

@StefH StefH self-assigned this May 30, 2016
@StefH StefH added the feature label May 30, 2016
@StefH
Copy link
Collaborator

StefH commented May 30, 2016

I'll copy the logic from https://github.com/kahanu/System.Linq.Dynamic/blob/master/Src/System.Linq.Dynamic/DynamicLinq.cs#L129

StefH added a commit that referenced this issue May 30, 2016
@Vlandmist
Copy link
Author

ok! that's great, I'll try it asap

StefH added a commit that referenced this issue May 31, 2016
@StefH
Copy link
Collaborator

StefH commented May 31, 2016

Code + tests have been added to Github.

Please take a look.
If you want a new NuGet, I can create this if needed.

@Vlandmist
Copy link
Author

thank you, yes a new nuget would be great to test it

@StefH
Copy link
Collaborator

StefH commented May 31, 2016

@StefH
Copy link
Collaborator

StefH commented Jun 5, 2016

Are you able to verify ?

@Vlandmist
Copy link
Author

I tried but I'm having problems, how can I retrieve a distinct list of Persons by name? (like the commented line). This is my code

            var lp = new List<Person>();
            lp.Add(new Person {Name = "John", Surname = "Lennon"});
            lp.Add(new Person {Name = "John", Surname = "Williams"});
            lp.Add(new Person {Name = "Paul", Surname = "MC"});
            lp.Add(new Person {Name = "Paul", Surname = "Allen"});
            lp.Add(new Person {Name = "Ringo", Surname = "Starr"});


             var qp = lp.AsQueryable().Select("(Name).Distinct");
            //var qp = lp.AsQueryable().Select(p => p.Name).Distinct();

            foreach (var n in qp)
            {
                Console.WriteLine(n.ToString());
            }

            Console.ReadKey();

gives a ParseException

An unhandled exception of type 'System.Linq.Dynamic.Core.Exceptions.ParseException' occurred in System.Linq.Dynamic.Core.dll
Additional information: No property or field 'Distinct' exists in type 'String'

@StefH
Copy link
Collaborator

StefH commented Jun 6, 2016

You can do this:
var qp = lp.AsQueryable().Select("Name").Distinct();

Or is the method you are looking for like:
var qp = lp.AsQueryable().Distinct("Name");
However that method does not exists (yet). I'll take a look if I can add this one.

@Vlandmist
Copy link
Author

Yes, your sample works, but I couldn't think of a dynamic use for it since Distinct is explicit. Also I can't seem to use Distinct inside a Where.("...") expression.

I'm working on a Linq to Entities (SQL Server) project using the original @NArnott library and some complex queries aren't easy to manage without support for Distinct and GroupBy methods.

@StefH
Copy link
Collaborator

StefH commented Jun 6, 2016

Can you provide an example for the _Where_ statement ?

Because _Select_ is supported : var result = queryable.Select("abc.Distinct()");

@Vlandmist
Copy link
Author

I'll answer with a sample asap. what is "abc" in your code sample?

@StefH
Copy link
Collaborator

StefH commented Jun 6, 2016

@StefH
Copy link
Collaborator

StefH commented Jun 8, 2016

Any luck?

@Vlandmist
Copy link
Author

Sorry mate, didn't have time to check it in these days. I'll write here when I have a couple more samples. keep on the good work

@StefH
Copy link
Collaborator

StefH commented Jun 27, 2016

Can you provide feedback ?

@Vlandmist
Copy link
Author

Ok let's forget about Distinct() support in Where. My situation was an extreme case and it's not worth replicating.

About Distinct in select I can't seem to make it work for simple lists (not nested object like your code sample)

This is my code:

        var integerList = new List<int>();
        integerList.AddRange(new []{1,1,2,3,4,4,5});

        //This works
        var xx = integerList.AsQueryable().Select("it").Distinct();

        //exception
        //Additional information: No property or field 'Distinct' exists in type 'Int32'
        var xy = integerList.AsQueryable().Select("it.Distinct()");`

        //exception
        //Additional information: No property or field 'Distinct' exists in type 'Int32'
        var xyz = integerList.AsQueryable().Select("Distinct(it)");

Maybe it's me that is not undertanding how this works. is there any way to achieve what I'm trying to do? if not, and if it's not useful to implement it, we can close this issue.

@StefH
Copy link
Collaborator

StefH commented Jul 15, 2016

Distinct does not solve your problem I think:

In your original question:

// real linq
var qp = lp.AsQueryable().Select(p => p.Name).Distinct();

// dynamic linq
var qpDynamic = lp.AsQueryable().Select("Name").Distinct();

The value from qp will be a List of Strings with the values : "John", "Paul" and "Ringo".

What you are trying do does not make sense I think, I have a feeling you want to use a groupby logic.


Note that real linq code like

var error = lp.AsQueryable().Select(p => p.Name.Distinct());

does also not compile.

StefH added a commit that referenced this issue Jul 15, 2016
@Vlandmist
Copy link
Author

Ok, thanks, that makes perfect sense, I'll close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants