none
Can I use Linq with an CollectionViewSource?

    Question

  • I've created a CollectionViewSource of work orders (_workOrdersView) so that I can filter work orders based upon different criteria (due date, quantity, etc).  My filter is working.  Now I want to execute a Linq query to loop through all the filtered work orders in the view to only get those in an "Available" status.  The query I'm attempting to use is:

    var availableWorkOrderIds = from w in _workOrdersView where w.GroupingStatus.Equals("Available"StringComparison.CurrentCultureIgnoreCase)                             select w.DatabaseWorkOrderID;

    foreach (int WorkOrderID in availableWorkOrderIds)
    {

    //additional logic...

    }

    but using _workOrdersView generates the error "Could not find an implementation of the query pattern for source type 'System.ComponetModel.ICollectionView'. 'Where' not found. Consider explicitly specifying the type of the range variable 'w'."

    I'm not sure what this error means or how to correct. Being new to WPF, CollectionViewSource, and Linq, I'm not sure I can even use Linq on a CollectionViewSource. Please let me know if what I'm trying to accomplish can be done using Linq and my CollectionViewSource and how or if not, how I should accomplish what I'm trying to do.

     

    Thank you.

    Friday, June 15, 2012 3:45 PM

Answers

  • Your Linq query in initial post is almost correct but just one modification needed. See below,

    var availWorkOrderIds = from w in _workOrdersView.OfType<WorkOrder>() 
                            where w.GroupingStatus.Equals("Available", StringComparison.CurrentCultureIgnoreCase)
                            select w.DatabaseWorkOrderID;

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Chris Basedau Dev Sunday, June 17, 2012 10:30 AM
    • Marked as answer by rsine Monday, June 18, 2012 11:05 AM
    Sunday, June 17, 2012 8:59 AM

All replies

  • For now, I am using the following:

    //only use workorders from the filtered view
    foreach (WorkOrder workorder in _workOrdersView)
    {
       if (workorder.GroupingStatus.Equals("Available", StringComparison.CurrentCultureIgnoreCase) == true)
       {

              //addtional logic....

        }
    }

    it does what I need but I'm not sure if it's the most elegant way to do it.  I'm still open to suggestions/guidance on how I could use Linq with my CollectionViewSource but in the meantime, the above will get me where I need to go.

    Friday, June 15, 2012 5:36 PM
  • Your Linq query in initial post is almost correct but just one modification needed. See below,

    var availWorkOrderIds = from w in _workOrdersView.OfType<WorkOrder>() 
                            where w.GroupingStatus.Equals("Available", StringComparison.CurrentCultureIgnoreCase)
                            select w.DatabaseWorkOrderID;

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Chris Basedau Dev Sunday, June 17, 2012 10:30 AM
    • Marked as answer by rsine Monday, June 18, 2012 11:05 AM
    Sunday, June 17, 2012 8:59 AM