从 NoSQL(特别是 Cosmos DB)查询不同的值无法转换为模型类型
Querying distinct values from NoSQL(Cosmos DB specifically) cannot convert to model type
总结
简而言之,我正在尝试显示所有不同的 ID。我试图理解为什么当我第一次 运行 我的查询(不是不同的)时,我能够将它们与我的模型一起使用,而不是现在。我收到的错误是 Cannot convert from System.String to AppName.Models.Student
.
例子
例如,显示我的 collection 中的所有 ID(文档中有重复的 ID):"SELECT s.ClassId from Students s"
工作正常,但是当我尝试获取不同的值时,它无法转换它们,使用查询是 SELECT DISTINCT VALUE s.ClassId from Students s"
这是我对 Cosmos DB 的查询:
public List<T> Query(string query)
{
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true };
// Execute query via SQL
IQueryable<T> QueryInSql = Client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(Database, Collection),
$"{query}",
queryOptions);
// convert Iqueryable to a list
return QueryInSql.ToList();
}
我更改了我的模型以仅包含
public class Class
{
public string ClassId {get; set;}
}
样本 collection 与文档
{
{
"id":"abc",
"studentName": "bob",
"classId":"en"
},
{
"id":"bcd",
"studentName": "billy",
"classId":"sp"},
{
"id":"sdf",
"studentName": "bart",
"classId":"en"
}
}
所以,错误出在我传入的查询中。向其中添加 VALUE
时,您会返回
["en","sp"]
然而我认为我得到的是
[{"id": "en"}, {"id": "sp"}]
总结
简而言之,我正在尝试显示所有不同的 ID。我试图理解为什么当我第一次 运行 我的查询(不是不同的)时,我能够将它们与我的模型一起使用,而不是现在。我收到的错误是 Cannot convert from System.String to AppName.Models.Student
.
例子
例如,显示我的 collection 中的所有 ID(文档中有重复的 ID):"SELECT s.ClassId from Students s"
工作正常,但是当我尝试获取不同的值时,它无法转换它们,使用查询是 SELECT DISTINCT VALUE s.ClassId from Students s"
这是我对 Cosmos DB 的查询:
public List<T> Query(string query)
{
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true };
// Execute query via SQL
IQueryable<T> QueryInSql = Client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(Database, Collection),
$"{query}",
queryOptions);
// convert Iqueryable to a list
return QueryInSql.ToList();
}
我更改了我的模型以仅包含
public class Class
{
public string ClassId {get; set;}
}
样本 collection 与文档
{
{
"id":"abc",
"studentName": "bob",
"classId":"en"
},
{
"id":"bcd",
"studentName": "billy",
"classId":"sp"},
{
"id":"sdf",
"studentName": "bart",
"classId":"en"
}
}
所以,错误出在我传入的查询中。向其中添加 VALUE
时,您会返回
["en","sp"]
然而我认为我得到的是
[{"id": "en"}, {"id": "sp"}]