entity-frameworkfilteringbindinglist

Filtering entity framework dbset before assigning it as datasource


I want to be able to do what is commented out below but I can not figure out how to do so. I understand why I need to do the load and then reference the local observable collection and set it to be a BindingList so that changes in the grid are kept in sync, but how can I do the filtering I want to do before assigning it? As it is now, I need to do the filtering within the grid which is not what I want to do.

Core.MyContext.SiteVisits.Load();
var siteVisits = Core.MyContext.SiteVisits.Local.ToBindingList();
//.Where(siteVisit => siteVisit.UPC == _site.UPC)
ugSiteVisits.DataSource = siteVisits;

Thanks very much for any help.


Solution

  • Instead of loading all the SiteVisits, just query the ones you want and add them to a the Local collection with IQueryable<T>.Load().

    Core.MyContext.SiteVisits.Where(siteVisit => siteVisit.UPC == _site.UPC).Load();
    ugSiteVisits.DataSource = Core.MyContext.SiteVisits.Local.ToBindingList();