简体   繁体   中英

Remove an object from List of dynamic object

//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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM