gpt4 book ai didi

c# - 使用 C# 和 linq 查询 mongo 中的嵌套属性

转载 作者:可可西里 更新时间:2023-11-01 09:54:59 25 4
gpt4 key购买 nike

我正在尝试使用 linq 查询来过滤来自 Mongo 的结果,但是我使用复杂对象的查询都不起作用:

以下工作正常:

query.Where(o => (o.Name == "Joe"))

但这给了我错误:

query.Where(o => (o.Address.HouseNumber == "1234"))

使用 2.1.1 版的 c# 驱动程序,如果我使用旧版驱动程序,我会得到:

Unable to determine the serialization information for the expression: p.Address.HouseNumber

at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary2 serializationInfoCache) at
MongoDB.Driver.Linq.Utils.BsonSerializationInfoHelper.GetSerializationInfo(Expression
node) at
MongoDB.Driver.Linq.PredicateTranslator.BuildComparisonQuery(Expression
variableExpression, ExpressionType operatorType, ConstantExpression
constantExpression) at
MongoDB.Driver.Linq.PredicateTranslator.BuildComparisonQuery(BinaryExpression
binaryExpression) at
MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression
expression) at
MongoDB.Driver.Linq.PredicateTranslator.BuildAndAlsoQuery(BinaryExpression
binaryExpression) at
MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression
expression) at MongoDB.Driver.Linq.SelectQuery.BuildQuery() at
MongoDB.Driver.Linq.SelectQuery.Execute() at
MongoDB.Driver.Linq.MongoQueryProvider.Execute(Expression expression)
at MongoDB.Driver.Linq.MongoQueryable
1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at
Core.Persistence.LegacyMongoDb.LegacyMongoDbImp.<QueryAsync>d__10
1.MoveNext() in C:\Users...\src\Core.Persistence.LegacyMongoDb\LegacyMongoDb.Implementation.cs:line 84

如果我使用当前版本的驱动程序,我会得到:

[Address].HouseNumber is not supported.

at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression expression) at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression) at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(BinaryExpression binaryExpression) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSkip(SkipExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateTake(TakeExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.Linq.MongoQueryProviderImpl1.Translate(Expression
expression) at
MongoDB.Driver.Linq.MongoQueryProviderImpl
1.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken) at MongoDB.Driver.Linq.MongoQueryableImpl2.ToCursorAsync(CancellationToken
cancellationToken) at
MongoDB.Driver.IAsyncCursorSourceExtensions.<ToListAsync>d__4
1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Core.Persistence.MongoDb.MongoDbImp.<QueryAsync>d__12
1.MoveNext() in C:\Users...\libs\mojio.core\src\Core.Persistence.MongoDb\MongoDb.Implementation.cs:line 68

我 super 卡住任何在正确方向上的帮助将不胜感激

编辑:

public class User : Base<User>, IUser
{
public string Name { get; set; }
public IAddress Address { get; set; }
}


public class Address : IAddress
{
public string HouseNumber { get; set; }
public string City { get; set; }
public string Country { get; set; }

}

最佳答案

我正在使用复杂的对象对其进行测试,它似乎对我有用。您使用的是哪个版本的 Mongo C# 驱动程序?您能否提供有关复杂对象的更多详细信息?也许列出这两个对象的属性会有所帮助。

关于c# - 使用 C# 和 linq 查询 mongo 中的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35190094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com