Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove an object from List of dynamic object

Tags:

c#

dynamic

linq

//Data is IList<ExpandoObject>
var result = (from dynamic item in Data
              where item.id== "123"
              select item).FirstOrDefault();

Want to achieve below feature but it is erroring out by saying remove not available for dynamic objects.

Data.Remove(result);

Let me know if any suggestions. thanks

like image 365
Kiran Waghule Avatar asked Sep 12 '25 21:09

Kiran Waghule


1 Answers

Error: remove not available for dynamic objects

base on Microsoft's docs, The Remove method of IList<T> accepts a parameter with the type of T:

ICollection<T>.Remove(T)

In your example, T is an ExpandoObject, so it means in the Remove method you should pass a parameter with the type of ExpandoObject but you didn't and you are passing a parameter with the type of dynamic. Therefore you facing this error

for resolving this you have two way:

1) Use explicit type instead of var:

ExpandoObject result = ...

2) cast the result when you are passing it to Remove:

Data.Remove((ExpandoObject) result)

I think with doing one of these ways, your problem will resolve. good luck.

like image 60
Hamed Moghadasi Avatar answered Sep 15 '25 12:09

Hamed Moghadasi