kbmMW LINQ #2 – Returning objects

Continuing on the previous LINQ post, I’ll in this post show how to return an object instance or a list of objects as result from a LINQ operation using kbmMW Linq.

Continuing on the previous LINQ post, I’ll just show a couple of more features possible using kbmMW’s Linq.

Returning an object or a list of objects as result

Remember in LINQ #1 where I showed how to work with classes and objects using kbmMW Linq? We already defined the following object. If you dont remember please read previous LINQ #1 blog post.

Again I add a number of instances to an object list, so we have something to work with.
But this time I will show how to get a TMyData instance back from kbmMW Linq alternatively how to get a list of matching TMyData back.

The returned objects are not references to existing objects, but are generated by the Linq framework, so you must remember to free them when they are no longer of interest.

If nothing match, nil will be returned.

This sample shows how to return object of the same type as the ones originally being in the Using clause. However you can use any class (registered with kbmMW) as a result. Only fields/properties that has matching names will be filled.

So having an object like this:

Remember to register the TMyData2 class along with the previously registered TMyData class:

While remembering that lst is still an instance of TObjectList<TMyData>, it’s perfectly legal to do:

You will be returned a TMyData2 object instance where only the Name and Address properties has values.

The same method can be applied to JSON, BSON, Messagepack, YAML and XML data too, which makes it a very easy thing to convert parts of structured documents to objects and object lists.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.