查看列表是否包含特定类型

时间:2019-03-06 16:29:17

标签: c#

请参见下面的类结构:

public interface ISport { ... }

public class Football: ISport { … }

public class Running : ISport { … }

和下面的客户端代码:

List<ISport> sports = new List<ISport>();
sports.Add(new Football());
sports.Add(new Running());

bool containsFootball = sports.Contains(typeof(Running));

我得到的错误是:

  

“无法从system.type转换为ISport”。

如何解决?

2 个答案:

答案 0 :(得分:7)

使用merged = pd.merge(invoices, supplier, on=['company id', 'company'], how='left') print(merged) company id company USD Invoice no Country 0 11 Company A 120000 1 USA 1 12 Company B 20000 2 USA 2 11 Company A 4000000 3 USA 3 11 Company A 99000 4 USA 4 13 Company C 8000 5 AUS 5 12 Company B 110000 6 USA 6 14 Company D 443000 7 MEX 之类的OfType<T>()

答案 1 :(得分:4)

只需使用Any并比较类型:

bool containsRunning = sports.Any(sport => sport is Running);