2014-11-05 22 views
3

我試圖檢索存儲在Azure DocumentDb中的單個實體。我發現我的代碼只有當我通過SQL提供的查詢,如下面的工作:CreateDocumentQuery在與LINQ一起使用時拋出異常,用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類(irrelevan牛逼的屬性和方法去除):

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做一些其他類型的coersion嗎?

+4

看起來就像是一個「修正正在進行」錯誤:https://social.msdn.microsoft.com/Forums/sqlserver/en -US/0b6db0dc-75da-45c2-86bf-aa325685232e /在guid-fields-in-documentdb查詢 - 論壇= AzureDocumentDB – 2014-11-05 16:19:52

+0

@RaphaëlAlthaus非常感謝 - 這似乎是錯誤。希望更新將很快發佈。如果您將其作爲答案發布,我會將其標記爲已接受。 – 2014-11-05 16:30:14

回答

1

在linq查詢中不支持對ToString方法的調用。嘗試解析id來GUID和做一個簡單的等式是這樣的:

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