Usher
Usher

Reputation: 2146

System.Data.DataRowCollection' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument

I am really stuck how to resolve this

 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;
    }

I have problem " tblThreatenedSpecies Subzone" to delete, it throws exception "Error occurred. Collection was modified; enumeration operation might not execute"

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);
                }
            }

I tried to modified based on what you guys advised but now i have different exception.

Hope someone guide me on the right path.

Upvotes: 1

Views: 5149

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460340

A DataRowCollection does not implement the generic IEnumerable<DataRow> but only the non-generic ÌEnumerable interface. That's why you cannot use the LINQ extension methods on DataTable.Rows directly.

You have to use DataTable.AsEnumerable or DataTable.Rows.Cast<DataRow>.

List<DataRow> rowList = objAssessment.tblThreatenedSpeciesSubzone.AsEnumerable().ToList();

Upvotes: 6

Related Questions