I am using the C# MongoDB driver version 2.1.0, and have the following issue: I have a static class that handles the db connection etc., where I expose properties as follows,
public static class StaticMongo {
private readonly static MongoClient Client;
private readonly static IMongoDatabase Database;
public static IMongoQueryable<Game> GetGames {
get {
return Database.GetCollection<Game>("Games").AsQueryable();
}
}
// ...
}
Each Game
has a list of Player
which contains a Name
property (and more, ofc). When accessing data, eg, get all Player
instances called _name
from Games
the following works:
var a = StaticMongo.GetHands.
Where(n => (int)n.GameType == i).
//Where(n => GamePassesFilter(n, true)).
SelectMany(n => n.Players).
Where(n => n.Name == _name).ToList();
But if I additionally want to filter them (ie, if I include the commented line), like this one,
private bool GamePassesFilter(Game _game, bool _manyAllowed) {
var _playersInGame = _game.Players.Count();
if (_manyAllowed && (_playersInGame == 5 ||_playersInGame == 6)) return true;
return false;
}
I get an exception. Why? And how should this be handled corretly?
In System.ArgumentException ist eine Ausnahme vom Typ "MongoDB.Driver.dll" aufgetreten, doch wurde diese im Benutzercode nicht verarbeitet.
Zusätzliche Informationen: Unsupported filter: value(MGM8.ViewModels.PlayersViewModel).GamePassesFilter([document]).
User functions in IMongoQueryable.Where
methods cannot be translated to FilterDefinition
in the driver's PredicateTranslator
.
If you are allowed to do this, you can use GamePassesFilter
by calling AsEnumerable()
before that:
StaticMongo.GetHands.AsEnumerable().Where(n => GamePassesFilter(n, true))...
Or, you can just write all the conditions in the query.
StaticMongo.GetHands.Where(n => n.Players.Count() == 5 || n.Players.Count() == 6 && _manyAllowed)...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.