Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中查询AsQueryable时获取InvalidOperationException#_C#_Linq_Mongodb_Asqueryable - Fatal编程技术网

C# 在C中查询AsQueryable时获取InvalidOperationException#

C# 在C中查询AsQueryable时获取InvalidOperationException#,c#,linq,mongodb,asqueryable,C#,Linq,Mongodb,Asqueryable,我有一个实体类作为城市 [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 s

我有一个实体类作为城市

 [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; }
我想用AsQueryable()类创建一个简单的查询。这是我的查询代码

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

线路。有人能解释一下为什么我会得到这个异常并找到解决方案吗?

该方法会查找与作为参数传递的表达式匹配的第一个结果。当它找不到任何异常时,将抛出此异常。如果不确定序列是否包含要查找的元素,请使用。请参阅文章以获得一个很好的摘要。

异常的消息是什么?它说“序列不包含任何元素”。但我调试了db连接成功,有一个集合是“city”。@vedat这意味着
集合
没有一个元素等于“VIENNA”。改用
FirstOrDefault
。@MártonMolnár感谢您的回答。我认为first documents的城市字段是“VIENNA”,但我理解first函数的功能是不同的。所以我用_id来查询。@Sohaty谢谢你的回答。
var query = collection.AsQueryable().First(c => c.city.Equals("VIENNA"));