Like XAMLPad, LINQPad enables us to quickly test our code - simple as hitting F5. What makes it more powerful is it's extensibility; for example in the following XML file that I have to export to a SQL Server database I have to migrate Name to FirstName and LastName.
<?xml version="1.0" standalone="yes"?>
<Newsletter>
<User>
<Email>Diane@Di-ForGod.com</Email>
<Name>Diane Kratochvil</Name>
<Relation>Robert's Mom</Relation>
<Validated>:)</Validated>
<guid>fbbf0f8a-91a9-4d5d-93a5-94b26b47943f</guid>
</User>
<User>
<Email>bill@global-webnet.com</Email>
<Name>Bill Kratochvil</Name>
<Relation>Robert's Dad</Relation>
<Validated>:)</Validated>
<guid>37922a89-b5d6-42bb-a34a-282ff5eea439</guid>
</User>
<User>
<Email>Koetting-cd@actx.edu</Email>
<Name>Cyndie Koetting</Name>
<Relation>Director</Relation>
<Validated>:)</Validated>
<guid>fc574089-4655-466e-8b52-485684b0e168</guid>
</User>
<User>
It was easy enough to use the statement let n=user.Element("Name").Value.Split()
var Users = from user in UsersXML.Descendants("User")
let n = user.Element("Name").Value.Split()
select new
{
FirstName=n[0],
LastName=n[1],
Email = user.Element("Email").Value,
However I ran into an error when the user only had registered with a single name; it was crashing on the n[1] index.
I trust this can be done in LINQ but time constraints and limited LINQ knowledge have me looking for a "reusable" solution. I created a new SplitElement extension that takes the current value, splits it and returns the specified offset - if the offset doesn't exist then return a space:
I use the namespace LINQPad for my extension so that I will simply have to provide a reference to my DLL and LINQPad will be able to find it - it is the namespace LINQPad uses.
Then all that remains is for me to hit F4 in LINQPad and attach my DLL.
Tags:
Categories: