cannot do asqueryable on mongodb collection

Daniel Gustafsson

I got two models, a User and a Team as 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've created a user but now I want to add teams 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 these 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

i3arnon

You are missing a namespace, MongoDB.Driver.Linq, simply add that at the top:

using MongoDB.Driver.Linq;

That specific method is:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Freeze a linq IQueryable (as a ToList().AsQueryable() would do)

MongoDB .NET driver: AsQueryable() is not found

How do I use the AsQueryable method asynchronously with MongoDb C# Driver 2.1?

Projection with AsQueryable in MongoDB C# driver 2.2

The method AsQueryable is not supported in the expression tree mongodb linq

How do I drop a database or collection using Hibernate OGM with MongoDB

Cannot find contents of the collection in mongodb using collection.find()

How to do lookup on an aggregated collection in mongodb that is being grouped?

How do I delete a field from a MongoDB collection by key?

How do I pass the objectID of a document inside of a MongoDB Collection in a URL?

How do I iterate over an entire MongoDB collection using mongojs?

Cannot get a covered query for sharded collection in MongoDB

In MongoDB, how do I print all collection counts and indexes?

How do I compare a value of a local MongoDB collection to a regular MongoDB collection?

How do I drop a MongoDB collection from the command line?

TypeError: Cannot read property 'collection' of null ---node to insertData with mongoDb

How do I insert the results of a cursor into a collection in the MongoDB shell?

Do not add document to collection if already exist in MongoDB

MongoDB .Net Linq GroupBy Child Array AsQueryable

How do I associate specific documents in a collection in MongoDB-atlas to documents that are stored in a different collection in the same cluster?

How to loop through a Collection and do operations - Mongodb, Nodejs

Mongodb AsQueryable() Performance

Cannot retrieve all entries from MongoDB collection

How do you consistently migrate a large MongoDB collection?

How do I get a Generic and static reference to a MongoDB collection?

How do I return all documents in mongodb collection?

MongoDB - How do I map a collection from a lookup?

How do I find the intersection between an array of objects and a collection in MongoDB?

MongoDB.Driver C# Cannot deserialize collection with Array of ObjectId

TOP Ranking

HotTag

Archive