如何过滤SelectList内容

时间:2019-04-30 16:09:35

标签: c# asp.net-mvc

如何过滤SelectList,以便仅在Classifier.Name ==“ CellTypes”的情况下获得ClassifierElements?甚至可行吗?我应该使用其他类型的对象,例如IEnumerable吗?

在CellController中:

ViewData["TypeId"] = new SelectList(_context.ClassifierElements, "Id", "Name");

以下是对象:

public class Cell
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int TypeId { get; set; }
    public ClassifierElement Type { get; set; }

    public ICollection<Component> Components { get; set; }

    public ICollection<Parameter> Parameters { get; set; }
}


public class Classifier
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<ClassifierElement> ClassifierElements { get; set; }
}

public class ClassifierElement
{
    public int Id { get; set; }

    public int ClassifierId { get; set; }
    public Classifier Classifier { get; set; }

    public string Name { get; set; }     
}

1 个答案:

答案 0 :(得分:1)

您可以使用Linq表达式来过滤列表:

_context.ClassifierElements.Where(e => e.Name == "CellTypes")