[CSHARP-468] Any() query on subdocument inside of Where() throws exception Created: 09/May/12  Updated: 20/Mar/14  Resolved: 09/May/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.4.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ryan Hoffman Assignee: Craig Wilson
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-413 Add support for Enumerable.Any in LIN... Closed

 Description   

Given these entities:

public class Person
{
public Guid Id

{ get; set; }
public string Name { get; set; }

public List<Address> Addresses

{ get; set; }
}

public class Address
{
public string Street { get; set; }

public string City

{ get; set; }
public string State { get; set; }

public string Zip

{ get; set; }

}

This query does not work: (people = MongoCollection<Person>)
people.AsQueryable().Where(p => p.Addresses.Any(a => a.State == "NY")).ToList()

It would be the equivalent of this query in the shell:
db.people.find(

{"Addresses.State":"NY"}

)

The exception thrown is:
Unhandled Exception: System.NotSupportedException: Enumerable.Any with a predicate is not supported.
at MongoDB.Driver.Linq.SelectQuery.BuildAnyQuery(MethodCallExpression methodCallExpression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 262
at MongoDB.Driver.Linq.SelectQuery.BuildMethodCallQuery(MethodCallExpression methodCallExpression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 695
at MongoDB.Driver.Linq.SelectQuery.BuildQuery(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 832
at MongoDB.Driver.Linq.SelectQuery.BuildQuery() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 123
at MongoDB.Driver.Linq.SelectQuery.Execute() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 132
at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\MongoQueryProvider.cs:line 147
at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\MongoQueryable.cs:line 81
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ConsoleApplication5.Program.Main(String[] args) in C:\Projects\ConsoleApplication5\ConsoleApplication5\Program.cs:line 55

For now, I am using the Inject() workaround, however this type of query I believe needs to be supported.



 Comments   
Comment by Craig Wilson [ 09/May/12 ]

This is scheduled for the 1.5 release and will result in an $elemMatch element getting generated.

Comment by Ryan Hoffman [ 09/May/12 ]

Another way to express this query is:
people.AsQueryable().Where(p => p.Addresses.Where(a => a.State == "NY").Any()).ToList();

This fails with this exception:
Unhandled Exception: System.ArgumentException: Unsupported where clause: Enumerable.Any<Address>(Enumerable.Where<Address>(p.Addresses, (Address a) => (a.State == "NY"))).
at MongoDB.Driver.Linq.SelectQuery.BuildQuery(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 862
at MongoDB.Driver.Linq.SelectQuery.BuildQuery() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 123
at MongoDB.Driver.Linq.SelectQuery.Execute() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 132
at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\MongoQueryProvider.cs:line 147
at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\MongoQueryable.cs:line 81
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ConsoleApplication5.Program.Main(String[] args) in C:\Projects\ConsoleApplication5\ConsoleApplication5\Program.cs:line 55

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