Tenho uma lista de contêineres e gostaria de saber como retornar uma lista com todos os contêineres que são valiosos.
Este é o meu código até agora:
ContainerType.cs:
public enum ContainerType
{
Normal,
Cooled,
Valuable
}
Container.cs:
public ContainerType Type;
public int Weight { set; get; }
public Container(ContainerType type, int weight)
{
Weight = weight;
Type = type;
}
Row.cs:
public List<Container> GetValuable(List<Container> List)
{
return List.OfType<ContainerType.Valuable>();
}
O método em linha dá estes 2 erros:
CS0426 The type name 'Valuable' does not exist in the type 'ContainerType'
CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Containervervoer.Models.ContainerType.Valuable>' to 'System.Collections.Generic.List<Containervervoer.Models.Container>'. An explicit conversion exists (are you missing a cast?)
Você pode usar a Where
função LINQ para fazer isso.
public List<Container> GetValuable(List<Container> list)
{
return list.Where(x => x.ContainerType == ContainerType.Valuable).ToList();
}
A ToList()
deve resolver o seu problema de qualidade.
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras