2014-06-12 16 views
7

Ho due modelli, un utente e una squadra come di seguito:non può fare AsQueryable sulla raccolta mongodb

[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; } 

Così ho creato un utente, ma ora voglio aggiungere squadre per il mio utente. Questo è il codice che sto utilizzando:

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" }); 

Ma quando faccio questo ottengo questi errori:

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

+0

'AsQueryable' è un metodo di estensione per IEnumerable' '' non MongoCollection' si dovrà creare una o forse c'è una classe di estensione LINQ per Mongo già –

risposta

9

Ti manca uno spazio dei nomi , MongoDB.Driver.Linq, è sufficiente aggiungere quello nella parte superiore:

using MongoDB.Driver.Linq; 
metodo

Questo specifico è:

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

ho aggiunto che ma ora dice: errore operatore '==' non può essere applicato a operandi di tipo 'MongoDB.Bson.ObjectId' e 'oggetto' \t sull'utente linea var = collection.AsQueryable() prima. (o => o._id == Sessione ["ID"]); –

+2

@DanielGustafsson Questo è un problema completamente diverso. Infatti _id è di ObjectID e Session ["ID"] è un oggetto. Se sei sicuro che sia in realtà un ObjectID, lanciala: o._id == (ObjectID) Sessione ["ID"] – i3arnon

1

mi è stato sempre lo stesso errore con la v2.3.0 conducente .NET. L'ho rimosso e installato v2.2.4 usando NuGet e ha funzionato. Errore riscontrato: Metodo non trovato: 'MongoDB.Driver.Linq.ImongoQueryable 1<!!0> MongoDB.Driver.IMongoCollectionExtensions.AsQueryable(MongoDB.Driver.IMongoCollection 1)'.

+0

Ho ricevuto lo stesso errore con te quando eseguo l'upgrade a 2.3.0 e torno alla 2.2. 4, funziona di nuovo. Potrebbe essere un bug con la versione 2.3.0. – zhimin

0

è necessario includere

using MongoDB.Driver.Linq; 
Problemi correlati