When programming with Vault, you end up dealing with arrays a lot. I find myself constantly looping through arrays trying to find the 1 object I am looking for. Or I have an array of objects that I need to convert to an array of IDs.
.NET 3.0 has a nice feature, LINQ, that makes these operations much easier than creating foreach loops all the time. It's basically a query syntax for .NET collections. If you are a fan of SQL, then you should have no trouble picking up LINQ. Even if you are unfamiliar with SQL, the syntax is pretty easy to work with.
For example, let's say you want to find a specific property definition in an array of PropDef objects.
| // C# PropDef [] fileProps = propSvc.GetPropertyDefinitionsByEntityClassId("FILE");
// use LINQ to query the array if (selection.Count() == 0) |
| ' VB.NET Dim fileProps As PropDef() = propSvc.GetPropertyDefinitionsByEntityClassId("FILE")
' use LINQ to query the array If selection.Count() = 0 Then |
Now let's convert from File array to ID array.
| File [] files = docSvc.GetLatestFilesByFolderId(root.Id, true); |
| Dim files As File() = docSvc.GetLatestFilesByFolderId(root.Id, True) ' use LINQ to get the ID values Dim fileIdArray As Long() = ids.ToArray() |
It can also sort things for you.
| PropInst[] propValues = propSvc.GetProperties(
// LINQ will sort for you |
| Dim propValues As PropInst() = _ ' LINQ will sort for you
Dim values1 As PropInst() = sortedValues.ToArray() |
Lambda expressions
In many cases, you can condense things further by using Lambda expressions. It's a bit of a trade-off though. The code is less readable, but you get that nice "I did it all in 1 line" feeling of satisfaction.
Here are the above examples C#, but in Lambda ( I don't think VB.NET supports Lambda):
| // find my a specific item in the array // convert to ID array // sort the array |
Like any other programming feature, there is potential for misuse of LINQ. I recommend reading up on the MSDN documentation for how to best use these new concepts. Personally, I suggest avoiding the 'var' keyword.

Subscribe