Henry Henry - 3 months ago 9
C# Question

C# Winform (Entity Framework) - Iterate thru DBLocal

I am working with a project modeled on this link: Databinding with WinForms

On the form load on the link, the tutorial set the binding to:

this.categoryBindingSource.DataSource =
_context.Categories.Local.ToBindingList();


my question is, is it possible to iterate rows and columns on DBlocal?
_context.Categories.Local
? If Yes, how?

Thanks in Advance.

Answer

DbSet<T>.Local property is ObservableCollection<T> and you can simply use a for/foreach loop. You can use either of these options:

foreach (Category item in context.Category.Local)
{
    //MessageBox.Show(item.Name);      
}
for (int i = 0; i < context.Category.Local.Count; i++)
{
    var item = context.Category.Local[i];
    //MessageBox.Show(item.Name);
}
context.Category.Local.ToList()
       .ForEach(item =>
       {
           //MessageBox.Show(item.Name);
       });

Don't forget to first load data to Local.