如何在列表 C# MVC 中检索特定值

How to retrieve specific value in list C# MVC

我想根据列表本身的特定 ID 从列表中检索特定值。

这是我的列表值

    public List<strucMaterial> PopulateStructMaterialData()
    {
      List<strucMaterial> populateStructure = new List<strucMaterial>();
        {

            populateStructure.Add(new strucMaterial(){Id = 1, ifOthers = "", materialId = 1, materialNm = "Bricks", structId = 1, structNm = "Walls", insuranceReqId = 0});
            populateStructure.Add(new strucMaterial(){Id = 2, ifOthers = "", materialId = 2, materialNm = "Concrete", structId = 1, structNm = "Walls", insuranceReqId = 0});
            populateStructure.Add(new strucMaterial(){Id = 3, ifOthers = "", materialId = 3, materialNm = "Woods", structId = 1, structNm = "Walls", insuranceReqId = 0});

            populateStructure.Add(new strucMaterial(){Id = 4, ifOthers = "", materialId = 2, materialNm = "Concrete", structId = 2, structNm = "Roof", insuranceReqId = 0});
            populateStructure.Add(new strucMaterial(){Id = 5, ifOthers = "", materialId = 4, materialNm = "Tiles", structId = 2, structNm = "Roof", insuranceReqId = 0});
            populateStructure.Add(new strucMaterial(){Id = 6, ifOthers = "", materialId = 5, materialNm = "Zinc", structId = 2, structNm = "Roof", insuranceReqId = 0});

            populateStructure.Add(new strucMaterial(){Id = 7, ifOthers = "", materialId = 3, materialNm = "Woods", structId = 3, structNm = "Floor", insuranceReqId = 0});
            populateStructure.Add(new strucMaterial(){Id = 8, ifOthers = "", materialId = 6, materialNm = "Reinforced Concrete", structId = 3, structNm = "Floor", insuranceReqId = 0});

        };
        ViewBag.populatebuilding = populateStructure;
        return populateStructure;
    }

我的视图模型

public class strucMaterial
{
    public int buildingInfoID { get; set; }
    public int Id { get; set; }
    public string ifOthers { get; set; }
    public int materialId { get; set; }
    public string materialNm { get; set; }
    public int structId { get; set; }
    public string structNm { get; set; }
    public int insuranceReqId { get; set; }
    public bool isSelected { get; set; }
}

我的观点

                  @{

               List<Insurance.ViewModels.strucMaterial> viewModelSM = ViewBag.populatebuilding;

                for (int i = 0; i < viewModelSM.Count; i++)
                {
                    @Html.Label(viewModelSM[i].structNm)
                    @Html.CheckBox("structureMat", new { @id = "structureMat", @value = viewModelSM[i].Id })
                    @Html.Label(viewModelSM[i].materialNm)
                  <br />
                }

             }

这就是我检索 materialID 和 structID 的方式

                            int idArray = Convert.ToInt32(structureMat[i]);                     
                        List<strucMaterial> populateID = PopulateStructMaterialData();


                        for (int s = 0; s < populateID.Count; s++ )
                        {

                            if (populateID[s].Id.Equals(idArray))
                            {
                                int matID = populateID[s].materialId;
                                int structID = populateID[s].structId;

                                int a = matID;

                            }
                  }

不是获取materidID和structID值,而是获取元素的索引值。我的问题是为什么它只读到 populateID[s]..?为什么不整体阅读 populate[s].materialId?

谢谢

评论补充 Hari Prasad:

您可以使用 LINQ 并编写 populateID.First(x=>x.Id == idArray).materialId