[CSHARP-419] AsQueryable<T>().ToList() throws "System.ArgumentOutOfRangeException was unhandled" exception Created: 29/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: Mennan Kara Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by CSHARP-420 Issue when try to access Collection.A... Closed

 Description   

public class OperationLog 
{
    public BsonObjectId _id { get; set; }
    public DateTime TimeCreated { get; set; }
    public string Area { get; set; }
    public string Controller { get; set; }
    public string Action { get; set; }
    public string Operation { get; set; }
}

const string connectionString = "mongodb://localhost";
const string databaseName = "applogs";
var server = MongoServer.Create(connectionString);
var database= server.GetDatabase(databaseName, SafeMode.Create(true)); 
if (!database.CollectionExists("OperationLog"))
{
    database.CreateCollection("OperationLog");
}
MongoCollection<OperationLog> collection = database.GetCollection<OperationLog>("OperationLog");
var noproblem = collection.AsQueryable<OperationLog>().Where(a => true).ToList(); //This line works without a problem
var problem = collection.AsQueryable<OperationLog>().ToList(); //This is where the exception occurs

System.ArgumentOutOfRangeException was unhandled
  HResult=-2146233086
  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 MyApp.ConsoleApp.Program.Main(String[] args) in D:\MyApp\MyApp.ConsoleApp\Program.cs:line 67
       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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
 



 Comments   
Comment by Mennan Kara [ 30/Mar/12 ]

Great, thanks a lot, posting another one

Comment by Robert Stam [ 30/Mar/12 ]

Other workarounds are to add Where(x => true) or Select(x => x).

Or get the latest from github where this is now fixed.

Comment by Mennan Kara [ 30/Mar/12 ]

Thanks.

Comment by Robert Stam [ 29/Mar/12 ]

Thanks for the report. I'll look into it.

If you are not doing any where or select you could use FindAll on the collection as a workaround.

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