Create an array of items in a Select
I have a class generated by XSD.EXE called workOrders
. The class has a member Items
of type workOrdersWorkOrder[]
. I want to populate workOrders
with LINQ.
I don't know how to populate my Items member properly. This is where I need help:
labels = result.AsEnumerable()//labels is of type workOrders
.Select(o => delegate()
{
new workOrders
{
new Items
{
number = o.WorkOrder,
开发者_如何学C Part = o.Part,
Col3 = o.Col3,
Qty = o.Quantity.ToString()
}
};
}).ToList();
Note that this code doesn't work and is probably way off. I wrote it that way to try and convey what I want to do.
In simpler cases (no member arrays) I have been able to do this via
.Select(o => new workOrder
{
number = o.WorkOrder,
Part = o.Part,
Col3 = o.Col3,
Qty = o.Quantity.ToString()
}).ToList();
But, in this case I don't know how to make the jump.
Thanks
If I'm understanding correctly, I think you need something more like this:
labels =
new workOrders
{
Items = result.Select(o => new workOrder
{
number = o.WorkOrder,
Part = o.Part,
Col3 = o.Col3,
Qty = o.Quantity.ToString()
}).ToArray()
};
You are almost right... the following might work:
labels = (from item in result.AsEnumerable()//labels is of type workOrders
select new workOrders
{
Items = new workOrdersWorkOrder[]
{
{
number = item.WorkOrder,
Part = item.Part,
Col3 = item.Col3,
Qty = item.Quantity.ToString()
}
}
}
}).ToList();
Haven't tried but it could work :). Also, it depends on what you data you have and what you expect as result. My code above will create a list of workOrders
where each object will have one item in the list Items
, but that might be correct.
精彩评论