[CSHARP-2546] Can't query URI types on scheme/host/path etc Created: 14/Mar/19  Updated: 27/Oct/23  Resolved: 19/Mar/19

Status: Closed
Project: C# Driver
Component/s: BSON, Linq
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: James Turner Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

With the URI type is serialized to string, you can only do direct comparison (equal/not equal) whereas in C#, the type has various more properties like scheme, host, path etc. Because of this, you can't do many useful queries against this type, making it kinda useless to even support serialization of the Uri type.

For example, take the following class:

class ExampleClass
{
    public string Id { get; set; }
    public Uri WebsiteUri { get; set; }
}

I can't query it like:

myCollection.AsQueryable().Where(e => e.WebsiteUri.Host == "somedomain.com");

Because

InvalidOperationException: {document}{WebsiteUri}.Host is not supported.

I can only do this:

myCollection.AsQueryable().Where(e => e.WebsiteUri == new Uri("http://somedomain.com/some-page"));



 Comments   
Comment by Robert Stam [ 19/Mar/19 ]

You have a good point about not fully supporting LINQ queries like this. I don't know if that means it's useless to support serializing URIs at all though.

If you need to do queries like that the easiest approach would be to create your own POCO that you would use instead of Uri, in which case the Uri could be serialized as document containing the various components of the URI.

For example:

public class MyUri
{
    public string Scheme { get; set; }
    public string Host { get; set; }
    public int Port { get; set; }  
    // etc...
}

which would serialize as

{ Scheme : "http", Host : "somehost.com", Port : 1234, ... }

In this case your LINQ query would be supported.

 

Generated at Wed Feb 07 21:42:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.