Skip to content

Allow using Guid and other types for queries #274

@olee

Description

@olee

I'm writing an application where I have many related entities and I use Guid most of the time as key type.
However, this causes a lot of issues with queries like the following:

            var userData = userCtx.Query<User>()
                .UseKeys(new string[] { userId.ToString() })
                .Select(u => new
                {
                    User = u,
                    Teams = teamCtx.Query<Team>().UseKeys(u.Teams.Select(x => x.ToString())).ToList(),
                    Groups = groupCtx.Query<Group>().UseKeys(u.Groups.Select(x => x.ToString())).ToList(),
                })
                .FirstOrDefault();

The generated query is explicitly performing an array mapping with the to-string function:

SELECT 
	`Extent1` as `user`, 
	(SELECT RAW `Extent2` FROM `team` as `Extent2` USE KEYS ARRAY TOSTRING(`Extent3`) FOR `Extent3` IN `Extent1`.`teams` END) as `teams`, 
	(SELECT RAW `Extent4` FROM `group` as `Extent4` USE KEYS ARRAY TOSTRING(`Extent5`) FOR `Extent5` IN `Extent1`.`groups` END) as `groups` 
FROM `user` as `Extent1` USE KEYS ['00000000-0000-0000-0001-000000000001'] 
LIMIT 1

Instead, the query I would expect to get should look like this:

SELECT 
	`Extent1` as `user`, 
	(SELECT RAW `Extent2` FROM `team` as `Extent2` USE KEYS `Extent1`.`teams`) as `teams`, 
	(SELECT RAW `Extent4` FROM `group` as `Extent4` USE KEYS `Extent1`.`groups`) as `groups` 
FROM `user` as `Extent1` USE KEYS ['00000000-0000-0000-0001-000000000001'] 
LIMIT 1

Instead I would like to be able to either directly write

teamCtx.Query<Team>().UseKeys(u.Teams).ToList()

or at least be able to use the Linq cast function like this to make it compile properly and generate an efficient query:

teamCtx.Query<Team>().UseKeys(u.Teams.Cast<string>()).ToList()

However this last variant is throwing an error: No coercion operator is defined between types 'System.Guid' and 'System.String'.

Is there any other way to achieve the query I intended or would this require an adjustment to this library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions