linq - getting InvalidOperationException while querying with AsQueryable in C# -


i have entity class city.

 [bsonrepresentation(mongodb.bson.bsontype.objectid)]         public string _id { get;  set; }         public string city { get; set; }         public array loc { get; set; }         public double pop { get; set; }         public string state { get; set; } 

and want create simple query asqueryable() class. here query code

string dbname = dao.dbname(); var db = mongo.getdatabase(dbname);  using (mongo.requeststart(db)) {        var collection = db.getcollection<city>("city");        var query = collection.asqueryable().first(c => c.city.equals("vienna"));         console.writeline( query.tostring()); } 

when run code system.invalidoperationexception

an unhandled exception of type 'system.invalidoperationexception' occurred in system.core.dll

at

var query = collection.asqueryable().first(c => c.city.equals("vienna")); 

line. can explain why i'm getting exception , lead solution?

the first method looks first result matches expression passed argument. when doesn't find any, throw exception. if not sure sequence contains element looking for, use firstordefault. see this article nice summary.


Comments