Getting an error when running the F# version of the code below, while the C# version below works. Any ideas about on how to return multiple documentdb properties from a LINQ query with F# in general?
2016-12-29T23:57:08.504 Exception while executing function: Functions.GetTags. mscorlib: Exception has been thrown by the target of an invocation. mscorlib: One or more errors occurred. Microsoft.Azure.Documents.Client: Constructor invocation is not supported.
C#
var userTagsQuery =
userDocument.Where(user => user.topics != null && user.education != null)
.Select(user => new {topics=user.topics, education=user.education});
F#
type UserTagRecord = {topics : string list; education : string list}
let userTagsQuery =
user.Where(fun user -> user.topics <> null && user.education <> null)
.Select(fun user -> {topics=user.topics :?> string list; education=user.education :?> string list})
The error says "Constructor invocation is not supported". This is a sensible limitation, most LINQ providers have it.
In your F# code, the constructor call is the record creation. F# records are compiled as classes with a bunch of read-only properties and a single constructor that takes values for those properties as parameters. According to the error message, calling this constructor is not supported. Bad luck.
An interesting thing to notice is that C# anonymous types (employed in your C# code) work in exactly same way as F# records - they are classes with a bunch of read-only properties and a single constructor, - and yet, they are supported. Handing C# anonymous types as a special case is, again, a common practice for LINQ providers. Many LINQ providers would handle it in a more generalized way that would cover F# records as well, but in this case it is apparently not the case. If I were you, I would open an issue about it.
What you can try is replace your record with a class with mutable properties, and construct it with property initialization syntax:
I would also go out on a limb here and suggest that you might have further trouble with using F# lists. First, DocumentDb might not like them by themselves, and second, I don't know what
user.topicsanduser.educationare, but I'm pretty sure that they're not a subclass ofstring list, so your casts will probably fail.