Filtering Entity Framework Data Through a Loop

I am new to EF. When we work with a datareader or dataset, we sometimes fill in the control value in a loop. as

  datareader dr=getdata()
  while(dr.read())
  {
    // in this loop we can populate control with value from datareader 
  }

  dataset ds =getdata()
  for(int i=0;i<=ds.tables[0].rows.count-1;i++)
  {
    // in this loop we can populate control with value from dataset
  }

so I just want to know when I work with EF, while I can iterate through the loop and populate the controls with a value.

Another question: how to iterate over zero in EF.

please help me with a code example to understand things. thank

+3
source share
2 answers

Here is an example of contrived code to show how you can achieve what you need.

// Get all the cars
List<Car> cars = context.Cars.ToList();

// Clear the DataViewGrid
uiGrid.Rows.Clear();

// Populate the grid
foreach (Car car in cars)
{
    // Add a new row
    int rowIndex = uiGrid.Rows.Add();
    DataGridViewRow newRow = uiGrid.Rows[rowIndex];

    // Populate the cells with data for this car
    newRow.Cells["Make"].Value = car.Make;
    newRow.Cells["Model"].Value = car.Model;
    newRow.Cells["Description"].Value = car.Description;

    // If the price is not null then add it to the price column
    if (car.Price != null)
    {
        newRow.Cells["Price"].Value = car.Price;
    }
    else
    {
        newRow.Cells["Price"].Value = "No Price Available";
    }
}
+2
source

Source: https://habr.com/ru/post/1796208/


All Articles