Using Linq.Dynamic where method with a dynamic list gives error

c# dynamic dynamic-linq linq

Question

I am transforming some JSON into a dynamic list by reading it. Here's my code:

dynamic data = JObject.Parse(response);
var result = data.result;
var result = ((IEnumerable)arr).Cast<dynamic>().ToList();

var id = result[0].id;
var filtereddata = result.Where("id==1");

The arrow

var filtereddata = result.Where("id==1");

gives an errorNo property or field 'id' exists in type 'Object while var id = result[0].id; seemed to be effective.

I'm processing the following JSON:

{
"count": 1,
"result": [
{
  "id": 11,
  "name": "Locations",

}]
}

If you require any other information, just let me know. I appreciate you taking the time.

Edit: even a tryvar filtereddata = result.Where(c=>c.id==1).Select("id"); utilizing lambda expression, yet the problem persists.

1
0
10/5/2016 12:02:13 PM

Accepted Answer

Dynamic LINQ is incompatible withdynamic LINQ to Objects would be effective, however since you are getting the filter as astring It is not relevant.

Use temporary anonymous projection as a workaround prior to applying the dynamicWhere and then choose the original item once more:

var filtereddata = result
    .Select(x => new { item = x, id = (int)x.id, name = (string)x.name })
    .Where("id==1")
    .Select(x => x.item);
2
10/5/2016 12:06:08 PM


Related Questions





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