System.Data.DataRowCollection'不包含'ToList'的定义,也没有扩展方法'ToList'接受第一个参数

时间:2014-10-16 21:36:25

标签: c#

我真的很难解决这个问题

 public bool DeleteVegetationZone(ref Assessment objAssessment, int VegetationZoneIDToDelete, string UserFullname, ref string ErrorMessage)
    {
        string RowFilter = @"VegetationZoneID=" + Convert.ToString(VegetationZoneIDToDelete);
        Assessment.tblVegetationZoneRow[] VegetationZoneRows = (Assessment.tblVegetationZoneRow[])objAssessment.tblVegetationZone.Select(RowFilter);
        if ((VegetationZoneRows != null) && (VegetationZoneRows.Length != 0))
        {
            if (VegetationZoneRows.Length == 1)
            {
                if (VegetationZoneRows[0].VegetationZoneID > 0)
                {
                    VegetationZoneRows[0].UpdatedBySystemUser = UserFullname;
                    VegetationZoneRows[0].SaveType = (int)EnumCollection.SaveType.RemoveOnly;
                }
                else
                {
                    VegetationZoneRows[0].Delete();
                    objAssessment.AcceptChanges();
                }

                //tblThreatenedSpeciesSubzone

                var list = objAssessment.tblThreatenedSpeciesSubzone.Rows.ToList();
               for (int i = 0; i < objAssessment.tblThreatenedSpeciesSubzone.Count; i++)
                {

                    foreach (Assessment.tblThreatenedSpeciesSubzoneRow ThreatenedSpeciesSubzoneRow in objAssessment.tblThreatenedSpeciesSubzone.Rows)
                    {
                        if (ThreatenedSpeciesSubzoneRow.VegetationZoneID == VegetationZoneIDToDelete)
                            DeleteThreatenedSpeciesSubzone(ref objAssessment, ThreatenedSpeciesSubzoneRow.ThreatenedSpeciesZoneID, UserFullname, ref ErrorMessage);
                    }
                }

                UpdateSpeciesGeoHabitatSurveyTime(ref objAssessment, UserFullname, ref ErrorMessage);
            }
            else
            {
                //Cannot have more than one row with same key
                ErrorMessage = "Error: More than one record found - Vegetation zone ID = " + Convert.ToString(VegetationZoneIDToDelete);
                return false;
            }
        }
        else
        {
            //Must have at least one row with same key
            ErrorMessage = "Error: Record not found - Vegetation zone ID = " + Convert.ToString(VegetationZoneIDToDelete);
            return false;
        }
        return true;
    }

我有问题&#34; tblThreatenedSpecies Subzone&#34;要删除,它会抛出异常&#34;发生错误。收集被修改;枚举操作可能无法执行&#34;

var list = objAssessment.tblThreatenedSpeciesSubzone.Rows.ToList();
           for (int i = 0; i < objAssessment.tblThreatenedSpeciesSubzone.Count; i++)
            {

                foreach (Assessment.tblThreatenedSpeciesSubzoneRow ThreatenedSpeciesSubzoneRow in objAssessment.tblThreatenedSpeciesSubzone.Rows)
                {
                    if (ThreatenedSpeciesSubzoneRow.VegetationZoneID == VegetationZoneIDToDelete)
                        DeleteThreatenedSpeciesSubzone(ref objAssessment, ThreatenedSpeciesSubzoneRow.ThreatenedSpeciesZoneID, UserFullname, ref ErrorMessage);
                }
            }

我试图根据你们的建议进行修改,但现在我有不同的例外。

希望有人引导我走上正确的道路。

1 个答案:

答案 0 :(得分:4)

DataRowCollection不实现通用IEnumerable<DataRow>,而只实现非通用ÌEnumerable接口。这就是为什么你不能直接在DataTable.Rows上使用LINQ扩展方法的原因。您必须使用DataTabe.AsEnumerableRows.Cast<DataRow>

var list = objAssessment.tblThreatenedSpeciesSubzone.AsEnumerable().ToList();