其他分享
首页 > 其他分享> > 循环访问实体框架

循环访问实体框架

作者:互联网

我是EF的新手.当我们使用数据读取器或数据集时,有时会在循环中填充控件的值.喜欢

  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
  }

所以我只想知道当我使用EF时,如何在循环中迭代并使用值填充控件.

另一个问题是如何在EF中检查null.

请帮助我提供示例代码以了解这些内容.谢谢

解决方法:

这是一个人为设计的代码示例,显示如何实现所需的功能.

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

标签:c,entity-framework
来源: https://codeday.me/bug/20191208/2093093.html