C# CosmosDb 查询错误 CS0266:无法将类型 'System.Linq.IQueryable<System.Collections.Generic.List<xxxx>' 隐式转换为
C# CosmosDb Query error CS0266: Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<xxxx>' to
我有这个 CosmosDb 调用来检索租约列表。
List<Lease> leases = null;
var response = client.CreateDocumentQuery<List<Lease>>(
UriFactory.CreateDocumentCollectionUri("demo", "Leases"),
"select * from c");
leases = response;
但我在 leases = response; 处收到 CS0266 错误
完整的错误消息(只是为了获得比代码更多的文字/细节)
无法将类型 'System.Linq.IQueryable<System.Collections.Generic.List<Demo.Shared.Lease>>' 隐式转换为 'System.Collections.Generic.List<Demo.Shared.Lease>'
不过,如果我尝试这样做,它对一条记录有效。好吧,没有异常,响应为空。
var response = this.client.CreateDocumentQuery<Lease>(
UriFactory.CreateDocumentCollectionUri("demo", "Users"), queryOptions)
.Where(f => f.lease == "3114/968").AsEnumerable().ToArray().FirstOrDefault();
租约class:
public class Lease
{
public string id { get; set; }
public string lease { get; set; }
public Rent rent { get; set; }
}
public class Rent
{
public string type { get; set; }
public DateTime? dateFrom { get; set; }
public DateTime? dateTo { get; set; }
public double? amountIncTax { get; set; }
public string datePaid { get; set; }
public double? amountExTax { get; set; }
public double? taxAmount { get; set; }
public string chequeNumber { get; set; }
public string comment { get; set; }
public string receiptNumber { get; set; }
public string createdBy { get; set; }
public DateTime? createdDate { get; set; }
public string modifiedBy { get; set; }
public DateTime? modifiedDate { get; set; }
}
Cosmos 数据库中的JSON:
{
"lease": "E16/445",
"rent": {
"type": "Rent Payment",
"dateFrom": "2015-04-09T00:00:00.0000000",
"dateTo": "2016-04-08T00:00:00.0000000",
"amountIncTax": 244.2,
"datePaid": "2015-04-01T00:00:00.0000000",
"amountExTax": 244.2,
"taxAmount": 0,
"chequeNumber": "ONLINE",
"comment": null,
"receiptNumber": "MT-210010243602",
"createdBy": "SBM\Sandy.Thuel",
"createdDate": "2015-04-21T07:36:56.1400000",
"modifiedBy": "SBM\Sandy.Thuel",
"modifiedDate": "2015-04-21T07:36:56.4830000"
},
"id": "337ed2fa-44f3-4474-9b51-923c6635fb11",
"r_id": etc,
}
请有任何想法,
TIA
您必须将 IQueryable 转换为 List
。您可以通过在 Linq 查询末尾添加 .ToList()
来实现。同时将类型更改为 <Lease>
。试试这个查询,
var response = client.CreateDocumentQuery<Lease>(
UriFactory.CreateDocumentCollectionUri("demo", "Leases"),
"select * from c").ToList();
我有这个 CosmosDb 调用来检索租约列表。
List<Lease> leases = null;
var response = client.CreateDocumentQuery<List<Lease>>(
UriFactory.CreateDocumentCollectionUri("demo", "Leases"),
"select * from c");
leases = response;
但我在 leases = response; 处收到 CS0266 错误 完整的错误消息(只是为了获得比代码更多的文字/细节)
无法将类型 'System.Linq.IQueryable<System.Collections.Generic.List<Demo.Shared.Lease>>' 隐式转换为 'System.Collections.Generic.List<Demo.Shared.Lease>'
不过,如果我尝试这样做,它对一条记录有效。好吧,没有异常,响应为空。
var response = this.client.CreateDocumentQuery<Lease>(
UriFactory.CreateDocumentCollectionUri("demo", "Users"), queryOptions)
.Where(f => f.lease == "3114/968").AsEnumerable().ToArray().FirstOrDefault();
租约class:
public class Lease
{
public string id { get; set; }
public string lease { get; set; }
public Rent rent { get; set; }
}
public class Rent
{
public string type { get; set; }
public DateTime? dateFrom { get; set; }
public DateTime? dateTo { get; set; }
public double? amountIncTax { get; set; }
public string datePaid { get; set; }
public double? amountExTax { get; set; }
public double? taxAmount { get; set; }
public string chequeNumber { get; set; }
public string comment { get; set; }
public string receiptNumber { get; set; }
public string createdBy { get; set; }
public DateTime? createdDate { get; set; }
public string modifiedBy { get; set; }
public DateTime? modifiedDate { get; set; }
}
Cosmos 数据库中的JSON:
{
"lease": "E16/445",
"rent": {
"type": "Rent Payment",
"dateFrom": "2015-04-09T00:00:00.0000000",
"dateTo": "2016-04-08T00:00:00.0000000",
"amountIncTax": 244.2,
"datePaid": "2015-04-01T00:00:00.0000000",
"amountExTax": 244.2,
"taxAmount": 0,
"chequeNumber": "ONLINE",
"comment": null,
"receiptNumber": "MT-210010243602",
"createdBy": "SBM\Sandy.Thuel",
"createdDate": "2015-04-21T07:36:56.1400000",
"modifiedBy": "SBM\Sandy.Thuel",
"modifiedDate": "2015-04-21T07:36:56.4830000"
},
"id": "337ed2fa-44f3-4474-9b51-923c6635fb11",
"r_id": etc,
}
TIA
您必须将 IQueryable 转换为 List
。您可以通过在 Linq 查询末尾添加 .ToList()
来实现。同时将类型更改为 <Lease>
。试试这个查询,
var response = client.CreateDocumentQuery<Lease>(
UriFactory.CreateDocumentCollectionUri("demo", "Leases"),
"select * from c").ToList();