Linq发布查询两个数据集

时间:2012-07-13 21:40:00

标签: c# asp.net linq

以下是我正在使用的代码:

        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>'

我相信这很容易解决,但我无法理解。这背后的逻辑是我需要根据传递给此方法的密钥查找工作订单,然后获取工作订单所具有的所有服务项并迭代它们以将它们放入组合框中。

1 个答案:

答案 0 :(得分: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(...);

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