Home > Back-end >  C# CosmosDb Query error CS0266: Cannot implicitly convert type 'System.Linq.IQueryable<Syste
C# CosmosDb Query error CS0266: Cannot implicitly convert type 'System.Linq.IQueryable<Syste

Time:03-01

I have this CosmosDb call to retrieve a list of leases.

List<Lease> leases = null;

var response = client.CreateDocumentQuery<List<Lease>>(
    UriFactory.CreateDocumentCollectionUri("demo", "Leases"), 
    "select * from c");

leases = response;

but I get the CS0266 error at leases = response; The full error message (just to get more words / details than code)

Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<Demo.Shared.Lease>>' to 'System.Collections.Generic.List<Demo.Shared.Lease>'

If I try this though, it works for one record. Well no Exception and the response is null.

var response = this.client.CreateDocumentQuery<Lease>(
    UriFactory.CreateDocumentCollectionUri("demo", "Users"), queryOptions)
                       .Where(f => f.lease == "3114/968").AsEnumerable().ToArray().FirstOrDefault();

The Lease 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; }
}

The JSON in Cosmos db:

{
"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,

}

enter image description here Any ideas please,

TIA

CodePudding user response:

You have to convert your IQueryable to a List. You can do this by adding .ToList() at the end of your Linq query. Also change the type as <Lease>. Try this query instead,

var response = client.CreateDocumentQuery<Lease>(
    UriFactory.CreateDocumentCollectionUri("demo", "Leases"), 
    "select * from c").ToList();
  • Related