Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ArrayList中的动态类_C#_.net_Entity Framework_Razor - Fatal编程技术网

C# ArrayList中的动态类

C# ArrayList中的动态类,c#,.net,entity-framework,razor,C#,.net,Entity Framework,Razor,我使用实体框架和razor,我有三个CategoryModel。它们是从相同的基类派生的。首先是ContentCategoryModel、ProdCategoryModel和NewsCategoryModel。 我的问题是如何对ArrayList只使用一个“foreach循环” @{ Layout = "~/Views/Shared/_ManageLayout.cshtml"; ArrayList catList = ViewBag.catList; } <ul> /*But

我使用实体框架和razor,我有三个CategoryModel。它们是从相同的基类派生的。首先是ContentCategoryModel、ProdCategoryModel和NewsCategoryModel。 我的问题是如何对ArrayList只使用一个“foreach循环”

@{
Layout = "~/Views/Shared/_ManageLayout.cshtml";

ArrayList catList = ViewBag.catList;

}

<ul>
/*But ArrayList can contain ContentCategoryModel or NewsCategoryModel or ProdCategoryModel, how can i use this foreach dynamically. because i don't want to use three different foreach*/
    @foreach (ContentCategoryModel cat in catList)
    {
        <li>
            @cat.CategoryName
        </li>
    }
</ul>
@{
Layout=“~/Views/Shared/_ManageLayout.cshtml”;
ArrayList catList=ViewBag.catList;
}
    /*但是ArrayList可以包含ContentCategoryModel或NewsCategoryModel或ProdCategoryModel,如何动态地使用这个foreach。因为我不想使用三种不同的foreach*/ @foreach(catList中的ContentCategoryModel猫) {
  • @类别名称
  • }

ArrayList
只保存对象,因此必须将其强制转换为类型才能有效地使用它。因为您有一个基类型,所以可以强制转换它:

@foreach (CategoryModel cat in catList.Cast<CategoryModel>())
{
    <li>
        @cat.CategoryName
    </li>
}
@foreach(catList.Cast()中的CategoryModel cat)
{
  • @类别名称
  • }
    或迭代三次以查找每种类型:

    @foreach (ContentCategoryModel cat in catList.OfType<ContentCategoryModel>())
    {
        <li>
            @cat.CategoryName
        </li>
    }
    
    @foreach (ProdCategoryModel cat in catList.OfType<ProdCategoryModel>())
    {
        <li>
            @cat.CategoryName
        </li>
    }
    
    @foreach (NewsCategoryModel cat in catList.OfType<NewsCategoryModel>())
    {
        <li>
            @cat.CategoryName
        </li>
    }
    
    @foreach(catList.OfType()中的ContentCategoryModel猫)
    {
    
  • @类别名称
  • } @foreach(catList.OfType()中的ProdCategoryModel cat) {
  • @类别名称
  • } @foreach(catList.OfType()中的NewsCategoryModel猫) {
  • @类别名称
  • }
    然后将它们转换为基类。没关系。谢谢。
    ArrayList
    只包含对象,因此
    cat
    将是一个
    对象。
    
    @foreach (ContentCategoryModel cat in catList.OfType<ContentCategoryModel>())
    {
        <li>
            @cat.CategoryName
        </li>
    }
    
    @foreach (ProdCategoryModel cat in catList.OfType<ProdCategoryModel>())
    {
        <li>
            @cat.CategoryName
        </li>
    }
    
    @foreach (NewsCategoryModel cat in catList.OfType<NewsCategoryModel>())
    {
        <li>
            @cat.CategoryName
        </li>
    }