ledgeJumper
ledgeJumper

Reputation: 3630

Linq issues querying two Collections of data

Here is the code I am working with:

        Collection<WorkOrderLabor> workOrder = new Collection<WorkOrderLabor>();
        Collection<ServiceItem> serviceItems = new Collection<ServiceItem>();

        serviceItems  = from si in serviceItems
                        join cw in workOrder on si.ServiceKey equals cw.Key
                        select new { si };

        foreach (ServiceItem item in serviceItems)
            ctrl.Items.Add(...);

I am getting this error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.ObjectModel.Collection<ServiceItem>'

I believe this to be hopefully an easy fix, but I can't figure it out. The logic behind this is I need to look up a Work Order based off of a key being passed to this method, and then get all the service Items that work order has and iterate through them to throw them in a combobox.

Upvotes: 0

Views: 57

Answers (1)

Russ Cam
Russ Cam

Reputation: 125488

Should just be

Collection<WorkOrderLabor> workOrder = new Collection<WorkOrderLabor>();
Collection<ServiceItem> serviceItems = new Collection<ServiceItem>();

var filteredItems = from si in serviceItems
                    join cw in workOrder on si.ServiceKey equals cw.Key
                    select si;

foreach (ServiceItem item in filteredItems)
    ctrl.Items.Add(...);

You don't want to return an IEnumerable<> of anonymous types from the LINQ expression but an IEnumerable<ServiceItem>

Upvotes: 5

Related Questions