Search code examples
c#odata

C# - Operator cannot be applied to operands of type 'XXX'?


I working a project .NET core 6.0 Console with Simple.OData.Client. Just try the sample code - ODATA Batch Request and got error:

Operator '+=' cannot be applied to operands of type 'ODataBatch' and 'lambda expression'

Very sorry if I'm missing something obvious, but when I attempt to create a batch using the untyped syntax shown on the Wiki, I get an error that states Operator '+=' cannot be applied to operands of type 'ODataBatch' and 'lambda expression'. The code I'm using is the example code from the Wiki, shown below:

var batch = new ODataBatch(serviceUri);
batch += c => c.InsertEntryAsync(
        "Products",
        new Entry()
        {
            { "ProductName", "Test1" },
            { "UnitPrice", 21m }
        },
        false);

await batch.ExecuteAsync();

I know I'm probably missing something simple and obvious here, but any help would be appreciated. I've tried a few things to make this work, but have failed to resolve the issue.

Thanks!


Solution

  • What does your Entry class look like?

    InsertEntryAsync expects to receive a string, a IDictionary<string, object> and a bool. So for your code to work, Entry needs to be an IDictionary<string, object>.

    What I think happens is c.InsertEntryAsync cannot be fully resolved because Entry is not a dictionary, and the lambda you write gets resolved as a lambda Expression instead of a Func<IODataClient, Task>. It needs to get resolved into the latter in order to use the + operator. See source

    This code at least compiled for me:

    var serviceUri = new Uri("asd");
    var dict = new Dictionary<string, object>();
    dict["ProductName"] = "Test1";
    dict["UnitPrice"] = 21m;
    var batch = new ODataBatch(serviceUri);
    batch += c => c.InsertEntryAsync(
            "Products",
            dict,
            false);
    
    await batch.ExecuteAsync();