DataReaders, LINQ to XML and Range Generation

I’m doing a bunch of database / XML stuff @ work, so I decided to use to VS08 beta 2 so I can use LINQ. For reasons I don’t want to get into, I needed a way to convert arbitrary database rows, read using a SqlDataReader, into XML. LINQ to SQL was out, since the code has to work against arbitrary tables (i.e. I have no compile time schema knowledge). But XLinq LINQ to XML helped me out a ton. Check out this example:

const string ns = "{http://some.sample.namespace.schema}";

while (dr.Read())
{
    XElement rowXml = new XElement(ns + tableName,
        from i in GetRange(0, dr.FieldCount)
        select new XElement(ns + dr.GetName(i), dr.GetValue(i)));
}

That’s pretty cool. The only strange thing in there is the GetRange method. I needed an easy way to build a range of integers from zero to the number of fields in the data reader. I wasn’t sure of any standard way, so I wrote this little two line function:

IEnumerable<int> GetRange(int min, int max)
{
    for (int i = min; i < max; i++)
        yield return i;
}

It’s simple enough, but I found it strange that I couldn’t find a standard way to generate a range with a more elegant syntax. Ruby has standard range syntax that looks like (1..10), but I couldn’t find the equivalent C#. Did I miss something, or am I really on my own to write a GetRange function?

Update: As expected, I missed something. John Lewicki pointed me to the static Enumerable.Range method that does exactly what I needed.

Morning Coffee 112

  • The Lee Holmes over at the Powershell Team Blog writes about alternatives to the “decades-old” Windows console host. Powershell Plus looks awesome. PoshConsole also looks pretty cool (though far from finished yet) and is free.
  • WL IDWeb Authentication SDK has been released. Details on the WL ID team blog. It looks like what Passport SDK provided for quite some time, but now it’s free. There’s also a client auth SDK in development. (via Dare Obasanjo)
  • Libor Soucek leaps to the wrong conclusion about not differentiating enterprise & support systems. Of course, different systems will have different availability requirements. But what happens when we connect them together? We can’t let the support system effect the availability of the enterprise system, right? To me, that implies either a) the support system now needs to conform to enterprise system availability requirements or b) we need some other mechanism (like async durable messaging) to act as a buffer between them. Personally, I like “b”.
  • Nick Carr points to an article The Trouble with Enterprise Software by Cynthia Rettig. Cynthia writes that while the massive complexity of enterprise software, especially large-scale ERP systems like SAP, significantly hinder it’s value. It’s a must read. Choice quotes:
    • “It is estimated that for every 25% increase in complexity in the tasks to be automated, the complexity of the software solution itself rises by 100%.”
    • “The notion of reusable software works on a small scale. Programmers have successfully built and reused subroutines of standard functions. But as software grows more complex, reusability becomes a difficult or impossible task.”
    • “Hope, unfortunately, has never been a very effective strategy.”
    • “Is enterprise software just too complex to deliver on its promises? After all, enterprise systems were supposed to streamline and simplify business processes. Instead, they have brought high risks, uncertainty and a deeply worrying level of complexity. Rather than agility they have produced rigidity and unexpected barriers to change, a veritable glut of information containing myriad hidden errors, and a cloud of questions regarding their overall benefits.”