Azure DocumentDb错误“查询必须评估为IEnumerable". [英] Azure DocumentDb error "Query must evaluate to IEnumerable"
问题描述
在尝试检索单个记录时,尝试查询我的Azure DocumentDb存储帐户时遇到问题.这是我的WebAPI代码:
I am having issues in trying to query my Azure DocumentDb storage account when attempting to retrieve a single record. This is my WebAPI code:
// Controller...
public AccountController : ApiController {
// other actions...
[HttpGet]
[Route("Profile")]
public HttpResponseMessage Profile()
{
var userId = User.Identity.GetUserId();
var rep = new DocumentRepository<UserDetail>();
var profile = rep.FindById(userId);
if (profile == null)
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Profile not found");
return Request.CreateResponse(HttpStatusCode.OK, profile);
}
}
// Repository
public class DocumentRepository<TEntity> : IDocumentRepository<TEntity> where TEntity : IIdentifiableEntity
{
private static DocumentClient _client;
private static string _databaseName;
private static string _documentsLink;
private static string _selfLink;
public DocumentRepository()
{
_client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["DocumentDbEndpointUrl"]), ConfigurationManager.AppSettings["DocumentDbAuthKey"]);
_databaseName = ConfigurationManager.AppSettings["DocumentDbDatabaseName"];
var _database = ReadOrCreateDatabase();
var collection = InitialiseCollection(_database.SelfLink, EntityName);
_documentsLink = collection.DocumentsLink;
_selfLink = collection.SelfLink;
}
// other methods...
public TEntity FindById(string id)
{
return _client.CreateDocumentQuery<TEntity>(_documentsLink).SingleOrDefault(u => u.Id.ToString() == id);
}
}
此FindById
方法导致以下问题:
Microsoft.Azure.Documents.Client.dll中发生了'Microsoft.Azure.Documents.Linq.DocumentQueryException类型的异常,但未在用户代码中处理
An exception of type 'Microsoft.Azure.Documents.Linq.DocumentQueryException' occurred in Microsoft.Azure.Documents.Client.dll but was not handled in user code
其他信息:查询表达式无效,表达式返回类型
不支持Foo.Models.DocumentDbEntities.UserDetail.查询必须计算为IEnumerable.
Additional information: Query expression is invalid, expression return type
Foo.Models.DocumentDbEntities.UserDetail is unsupported. Query must evaluate to IEnumerable.
我不知道此错误的含义或解决方法.我不希望返回IEnumerable
或任何后代类,因为此方法将返回0
或1
记录.如果我删除SingleOrDefault
子句,并将返回类型更改为IQueryable
,则它起作用,但这不是我想要的.
I don't understand what this error means, or how I fix it. I don't wish to return an IEnumerable
or any descendant class as this method will return either 0
or 1
records. It works if I remove the SingleOrDefault
clause, and change the return type to an IQueryable
, however this is not what I want.
推荐答案
SingleOrDefault()
.
将此更改为.Where(u => u.Id.ToString() == id).AsEnumberable().FirstOrDefault();
这篇关于Azure DocumentDb错误“查询必须评估为IEnumerable".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!