Conrad Clark
Conrad Clark

Reputation: 4526

C# Cannot convert lambda expression to type 'dynamic' because it is not a delegate type

Suppose I have a

List<dynamic> myList = new List<dynamic>();

Inside a class:

public class DynamicMixin : DynamicObject
{
    internal List<dynamic> myList= new List<dynamic>();

    public void AddInterface<T>(T _item) where T:class{
        Interfaces.Add(_item);
    }

    public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
    {
        if (myList.Contains((item)=>item.GetType().Equals(indexes[0].GetType())){
            /* do something */
        }
        return base.TryGetIndex(binder, indexes, out result);
    }

}

I'm trying to write myDynamicObject[typeof(IDisposable)] So I would get the IDiposable object which belongs to myDynamicObject.

This line gives me an error:

if (myList.Contains((item)=>item.GetType().Equals(indexes[0].GetType())){

Cannot convert lambda expression to type 'dynamic' because it is not a delegate type

I'm able to do it by iterating through the list: But why I'm not capable of using Contains ?

Upvotes: 3

Views: 3857

Answers (3)

Andrey
Andrey

Reputation: 60065

Becasue Contains is declared as:

public bool Contains(
    T item
)

You shoud use Any(your lambda)

Upvotes: 6

Bala R
Bala R

Reputation: 108957

IEnumerable<T>.Contains() does not have an overload that takes a lambda.

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160922

Contains() expects an actual item (of type dynamic in your case) not a delegate, I think you want Any() :

 if (myList.Any( item => item.GetType().Equals(indexes[0].GetType()))
 {

Upvotes: 7

Related Questions