I want to create a class which wraps a list of structures.
I have the following code:
public struct MyData
{
public int ID;
public string Description;
}
public class MyClass
{
private List<MyData> data;
public bool Contains(string desc)
{
if (data != null)
{
return data.Contains(item => item.Description.Contains(desc));
}
return false;
}
}
I can't see why I'm unable to use a Lambda expression, the error that I get is:
Cannot convert lambda expression to type 'MyApp.MyData' because it is not a delegate type
public bool Contains( string desc )
{
return data != null && data.Exists( item => item.Description.Contains( desc ) );
}
The reason is because the List<T>Contains
method expects a T
, whereas you've given it a lambda expression, created with the =>
.
What you could do is:
data.Any(item => item.Description.Contains(desc));
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.