Samuel Smith
Samuel Smith

Reputation: 149

How to fix "An expression may not contain a dynamic operation" in Linq?

I have a PUT Request where I passed in a json string value that contains an ID value and the other variables and values. After which I'll deserialize it and find the ID from the json in the Database, and then update the values:

 var bookininput = JsonConvert.DeserializeObject<dynamic>(value);

 if (bookininput.id == null || bookininput.id == 0)
 {
     return BadRequest("no id provided");
 }
 else { 
     var log = _context.BIBOLogs.Where(input => input.Id == bookininput.id); 
 }

However, I'm facing issues in .Where(input => input.Id == bookininput.id); because it's telling me that bookininput.id is dynamic and cannot be used for lookup. Any other methods to go around it?

Upvotes: 0

Views: 120

Answers (1)

EzLo
EzLo

Reputation: 14189

Try using a known class/type when doing your filter instead of dynamic. I'm assuming that the bookininput.id is int on the following example:

var bookininput = JsonConvert.DeserializeObject<dynamic>(value);
if (bookininput.id == null || bookininput.id == 0)
{
    return BadRequest("no id provided");
}
else 
{
    int inputId = int.Parse(bookininput.id);

    var log = _context.BIBOLogs
        .Where(input => input.Id == inputId);
}

Upvotes: 3

Related Questions