Wednesday, 19 November 2008

Now you've flatterned your data - what if you want to return it from a function

To make your flattened data more reuseable you may want to return it from a function. In the previous example it was an anonymous type. This is pretty straight forward - you just project it into your own type. Unfortunately you have to create it yourself but once it's done you don't have to do it again.

Dim bln As Boolean = False
Dim lst = (From t In ctx.OrderLine.Expand("Orders").Expand("Orders/Invoices") Where t.Completed.Equals(bln) And t.Username.ToLower = Trim(strUserName.ToLower)).ToList

Dim lst_aliased as List (of CustomTypes.CustomOrder) = (From t In lst Select New CustomTypes.CustomOrder With _
{.OrderLineQuantity = t.Quantity, .OrderDate = t.Orders.OrderDate _
.InvoiceNo = t.Orders.Invoices.InvoiceNo})

we can now return lst_aliased from a function as it is no longer an anonymous type


There are some other features here - mainly the use of expand to include the related data.

No comments: