ra0o0f / arangoclient.net

ArangoDB .NET Client with LINQ support
Apache License 2.0
99 stars 37 forks source link

Count/Length Function #94

Open bobmcallan opened 6 years ago

bobmcallan commented 6 years ago

Please implement Collection Count/Length (AQL: "return LENGTH(Collection)).

ra0o0f commented 6 years ago

do it like this:

string collectionName = db.NameOf<Collection>();
            var result= db.Query()
                .Select(_ => AQL.Length(collectionName))
                .First();
bobmcallan commented 6 years ago

Thanks.

bobmcallan commented 6 years ago

However, the result from Arango is not correct.

return length ( @p)
@p = 'Event'

= 5 (?)

return length ( Event)

= 654321 (the correct number)

ra0o0f commented 6 years ago

@bobmcallan you are right, it will return length of "Event" string, for now you should use COLLECTION_COUNT() method which is not implemented in the client right now, but you can use it by adding it as custom AQL method

    public class CustomAQL
    {
        [UserFunction(Name = "collection_count")]
        public static long CollectionCount(string collectionName)
        {
            throw new Exception("use custom AQL functions inside linq queries");
        }
    }

            // usage
            var result = db.Query()
                .Select(_ => CustomAQL.CollectionCount(collectionName))
                .First();
bobmcallan commented 6 years ago

I ended up creating a Length Statement.

var query = string.Format("Return {{ result : LENGTH({0}) }}", collectionName); var cntr = db.CreateStatement(query);

Similar to your suggestion.