[CSHARP-420] Issue when try to access Collection.AsQueryable<T> when no query is specified Created: 30/Mar/12  Updated: 02/Apr/15  Resolved: 30/Mar/12

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

Type: Bug Priority: Minor - P4
Reporter: Nick Cipollina Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: linq,query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Window 7 - 64 Bit, Visual Studio 2010


Issue Links:
Duplicate
duplicates CSHARP-419 AsQueryable<T>().ToList() throws "Sys... Closed

 Description   

This code isn't working. The
Employee class is defined as follows for the code example:

public class Employee
{
public ObjectId id

{ get; set; }

[BsonElement("fn")]
public string FirstName { get; set; }

}

If I do the following, I get an exception on the ToList() line:

var connectionString = "mongodb://localhost/sportsstore?
slaveOk=true";
var databaseName =
MongoUrl.Create(connectionString).DatabaseName;
var server = MongoServer.Create(connectionString);
var db = server.GetDatabase(databaseName);

var eCollection = db.GetCollection<Employee>("employees");
eCollection.Drop();
eCollection.Insert(new Employee

{FirstName = "Nick"}

);

var employees =
eCollection.AsQueryable<Employee>().ToList();

The exception I get is this:

System.ArgumentOutOfRangeException was unhandled
Message=Specified argument was out of the range of valid values.
Parameter name: Unable to find root IQueryable
Source=MongoDB.Driver
ParamName=Unable to find root IQueryable
StackTrace:
at
MongoDB.Driver.Linq.MongoQueryTranslator.GetDocumentType(Expression
expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq
\Translators\MongoQueryTranslator.cs:line 80
at
MongoDB.Driver.Linq.MongoQueryTranslator.Translate(MongoQueryProvider
provider, Expression expression) in C:\work\10gen\mongodb\mongo-csharp-
driver\Driver\Linq\Translators\MongoQueryTranslator.cs:line 50
at MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression
expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq
\MongoQueryProvider.cs:line 146
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 MongoQueryTester.Program.Main(String[] args) in c:\users
\ncipollina\documents\visual studio 2010\Projects\MongoQueryTester
\MongoQueryTester\Program.cs:line 27
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Now if I change the line that has ToList() to this it works:

var employees = eCollection.FindAll().AsQueryable().ToList();


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