Home > Software design >  C# MongoDB Driver 2.15.0 unable to execute In Query with Linq
C# MongoDB Driver 2.15.0 unable to execute In Query with Linq

Time:04-18

I am trying to get my resultset from MongoDB. I need to use In filter here. Here is my code that tries to get data.

IMongoCollection<VaersVax> vaersVaxCollection = mongoDatabase.GetCollection<VaersVax>("VaersVax");
var query = vaersVaxCollection.AsQueryable().Where(x => x.VAX_MANU.In(vaxManuList) && x.VAX_NAME.In(vaxNameList) && x.VAX_TYPE.In(vaxTypeList));
var list = query.Select(x => x.VAERS_ID).ToHashSet();

When I try to get a result i am getting the error;

System.ArgumentException: Unsupported filter: {document}{VAX_MANU}.In(value(System.String[])).
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.TranslateAndAlso(BinaryExpression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.TranslateAndAlso(BinaryExpression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.QueryableTranslator.TranslateWhere(WhereExpression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.QueryableTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.QueryableTranslator.TranslateSelect(SelectExpression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.QueryableTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.QueryableTranslator.Translate(Expression node)
   at MongoDB.Driver.Linq.Linq2Implementation.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
   at MongoDB.Driver.Linq.Linq2Implementation.MongoQueryProviderImpl`1.Translate(Expression expression)
   at MongoDB.Driver.Linq.Linq2Implementation.MongoQueryProviderImpl`1.Execute(Expression expression)
   at MongoDB.Driver.Linq.Linq2Implementation.MongoQueryableImpl`2.GetEnumerator()
   at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
   at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToHashSet[TSource](IEnumerable`1 source, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToHashSet[TSource](IEnumerable`1 source)
   at MongoOperations.MongoHelper.getTop10MostFrequentSymptom1DataByVaxManuNameType(IEnumerable`1 vaxManuList, IEnumerable`1 vaxNameList, IEnumerable`1 vaxTypeList) in D:\KodUygulamalari\OkulProje\VaersWebAPI\MongoOperations\MongoHelper.cs:line 318
   at MongoBusiness.MongoBusinessOperations.getTop10MostFrequentSymptom1DataByVaxManuNameType(IEnumerable`1 vaxManuList, IEnumerable`1 vaxNameList, IEnumerable`1 vaxTypeList)

Can I somehow execute a LINQ query on the MongoDB driver or should I be checking for different approaches?

CodePudding user response:

MongoDB.Driver support linq. for example:

IMongoCollection<VaersVax> vaersVaxCollection = mongoDatabase.GetCollection<VaersVax>("VaersVax");
var query = vaersVaxCollection.AsQueryable().Where(x => vaxManuList.Contains(x.VAX_MANU) && vaxNameList.Contains(x.VAX_NAME) && vaxTypeList.Contains(x.VAX_TYPE));
var list = query.Select(x => x.VAERS_ID).ToHashSet();
  • Related