Cannot request mongodb collection

I have two models: user and command, as shown below:

[BsonRepresentation(BsonType.ObjectId)] public ObjectId _id { get; set; } [Display(Name = "Password:")] public string Password { get; set; } [Display(Name = "Confirm:")] public string ConfirmPassword { get; set; } [Display(Name = "Email:")] public string Email { get; set; } [Display(Name = "Username:")] public string UserName { get; set; } [Display(Name = "Firtname:")] public string Firstname { get; set; } [Display(Name = "Lastname:")] public string Lastname { get; set; } [Display(Name = "Country:")] public string Country { get; set; } [Display(Name = "City:")] public string City { get; set; } [Display(Name = "Birthdate:")] public int Birthdate { get; set; } public List<Team> Teams { get; set; } [BsonRepresentation(BsonType.ObjectId)] public ObjectId TeamID { get; set; } public string TeamName { get; set; } public string UserName { get; set; } public int LeagueID { get; set; } public List<Player> Player { get; set; } 

So, I created a user, but now I want to add commands to my user. This is the code I'm using:

  var databaseClient = new MongoClient(Settings.Default.FantasySportsConnectionString); var server = databaseClient.GetServer(); var database = server.GetDatabase("Users"); var collection = database.GetCollection<User>("users"); var user = collection.AsQueryable().First(o => o._id == Session["ID"]); user.Teams.Add(new Team { TeamID = new ObjectId(), TeamName = "Some Team" }); 

But when I do this, I get the following errors:

1: Instance argument: cannot convert from 'MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>' to 'System.Collections.IEnumerable'

2: 'MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>' does not contain a definition for 'AsQueryable' and the best extension method overload 'System.Linq.Queryable.AsQueryable(System.Collections.IEnumerable)' has some invalid arguments

+7
c # linq mongodb mongodb-.net-driver
source share
3 answers

You are missing a namespace, MongoDB.Driver.Linq , just add this at the top:

 using MongoDB.Driver.Linq; 

This particular method:

 LinqExtensionMethods { public static IQueryable<T> AsQueryable<T>(this MongoCollection<T> collection); //... } 
+9
source share

I was getting the same error with the .NET v2.3.0 driver. I uninstalled it and installed v2.2.4 using NuGet and it worked. The error I kept getting: Method not found: "MongoDB.Driver.Linq.IMongoQueryable 1<!!0> MongoDB.Driver.IMongoCollectionExtensions.AsQueryable(MongoDB.Driver.IMongoCollection 1) '.

+1
source share

you need to enable

 using MongoDB.Driver.Linq; 
0
source share

All Articles