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

c# entity-framework-core linq

Question

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?

1
0
2/22/2019 11:05:31 AM

Popular Answer

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);
}
3
2/22/2019 11:07:04 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow