gpt4 book ai didi

c# - CreateDocumentQuery 与 LINQ 一起使用时会引发异常,与 SQL 一起使用时则正常

转载 作者:行者123 更新时间:2023-11-30 12:27:07 27 4
gpt4 key购买 nike

我正在尝试检索存储在我的 Azure DocumentDb 中的单个实体。我发现我的代码在我通过 SQL 提供查询时才有效,如下所示:

var query = String.Format("SELECT * FROM UserDetail u WHERE u.id = '{0}'", id);
return _client.CreateDocumentQuery<TEntity>(_selfLink, query).AsEnumerable().FirstOrDefault();

但是,使用 LINQ 表达式(如以下两个示例所示),它会失败并出现异常:

// EXCEPTION
return _client.CreateDocumentQuery<TEntity>(_selfLink).Where(u => u.Id.ToString() == id).AsEnumerable().FirstOrDefault();

// EXCEPTION
return (from u in _client.CreateDocumentQuery<TEntity>(_selfLink)
where u.Id.ToString() == id
select u).AsEnumerable().FirstOrDefault();

引发异常的相当可怕的堆栈跟踪是:

System.AggregateException: One or more errors occurred. ---> Microsoft.Azure.Documents.Linq.DocumentQueryException: Unhandled expression type: 'Call'
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarExpression(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitBinary(BinaryExpression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarExpression(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarLambda(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitWhere(ReadOnlyCollection`1 arguments, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitMethodCall(MethodCallExpression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.Translate(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.TranslateQuery(Expression inputExpression)
at Microsoft.Azure.Documents.Linq.SQLTranslator.TranslateQuery(Expression inputExpression)
at Microsoft.Azure.Documents.Linq.DocumentQueryEvaluator.HandleMethodCallExpression(MethodCallExpression expression, QueryType defaultQueryType, QueryType& queryType)
at Microsoft.Azure.Documents.Linq.DocumentQueryEvaluator.Evaluate(Expression expression, QueryType defaultQueryType, QueryType& queryType)
at Microsoft.Azure.Documents.Linq.DocumentQueryExecutionContext.<ExecuteAllAsync>d__7.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.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.Linq.DocumentQuery`1.<GetEnumeratorTAsync>d__10.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at Microsoft.Azure.Documents.Linq.DocumentQuery`1.GetEnumerator()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at Cherish.Domain.Repositories.Implementation.DocumentRepository`1.FindById(String id) in \\psf\home\Documents\Visual Studio 2013\Projects\Cherish\Cherish.Domain\Repositories\Implementation\DocumentRepository.cs:line 82
---> (Inner Exception #0) Microsoft.Azure.Documents.Linq.DocumentQueryException: Unhandled expression type: 'Call'
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarExpression(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitBinary(BinaryExpression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarExpression(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarLambda(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitWhere(ReadOnlyCollection`1 arguments, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitMethodCall(MethodCallExpression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.Translate(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Documents.Linq.ExpressionToSql.TranslateQuery(Expression inputExpression)
at Microsoft.Azure.Documents.Linq.SQLTranslator.TranslateQuery(Expression inputExpression)
at Microsoft.Azure.Documents.Linq.DocumentQueryEvaluator.HandleMethodCallExpression(MethodCallExpression expression, QueryType defaultQueryType, QueryType& queryType)
at Microsoft.Azure.Documents.Linq.DocumentQueryEvaluator.Evaluate(Expression expression, QueryType defaultQueryType, QueryType& queryType)
at Microsoft.Azure.Documents.Linq.DocumentQueryExecutionContext.<ExecuteAllAsync>d__7.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.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.Linq.DocumentQuery`1.<GetEnumeratorTAsync>d__10.MoveNext()<---

作为引用,这是 UserDetail 类(删除了不相关的属性和方法):

public class UserDetail : IIdentifiableEntity
{
[JsonProperty(PropertyName = "id")]
public Guid Id { get; set; }

[JsonProperty(PropertyName = "fn")]
public string Firstname { get; set; }

[JsonProperty(PropertyName = "ln")]
public string Lastname { get; set; }

[JsonProperty(PropertyName = "contribs")]
public IList<ContributorRelation> Contributors { get; set; }
}

我知道我可以使用 SQL 格式的方法,但是作为个人偏好,我想使用 Lambda,并且不明白为什么我的代码不能正常工作。我需要从 Guid 进行其他类型的类型强制转换吗?

最佳答案

linq 查询内部不支持对 ToString 方法的调用。尝试将 id 解析为 guid 并执行如下简单的相等操作:

return _client.CreateDocumentQuery<TEntity>(_selfLink).Where(u => u.Id == Guid.Parse(id)).AsEnumerable().FirstOrDefault();

关于c# - CreateDocumentQuery 与 LINQ 一起使用时会引发异常,与 SQL 一起使用时则正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26756590/

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