0

这是我正在使用的代码:

        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(...);

我收到此错误:

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

我相信这有望成为一个简单的解决方案,但我无法弄清楚。这背后的逻辑是我需要根据传递给此方法的键来查找工作订单,然后获取工作订单具有的所有服务项目并遍历它们以将它们放入组合框中。

4

1 回答 1

5

应该只是

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(...);

您不想IEnumerable<>从 LINQ 表达式返回一个匿名类型,而是返回一个IEnumerable<ServiceItem>

于 2012-07-13T21:42:12.773 回答