How many of you out there have heard of LINQ or (better yet) have used it? I believe it stands for Language INtegrated Query. Here's an example: It takes the multiple instances of <envelope> structures in an XML file and copies the data to an indexed object.
var envelopes =
from d in doc.Elements("envelope")
select new Envelope((string)d.Attribute("name"), Int16.Parse((string)d.Element("percent")), Double.Parse((string)d.Element("amount")));
int index = 0;
foreach (Envelope env in envelopes)
{
box[index] = env;
index++;
}
Another use I've put LINQ to is taking the "name" attribute values of all the "envelopes" and stuffed them in a ListBox:
var names =
from d in doc.Elements("envolpe")
select (string)d.Attribute("name");
foreach (string attr in names)
lstEnvelopes.Items.Add(attr);
If you want to know what "doc" is exactly, here is the declaration:
XElement doc = XElement.Load(Constants.BUDGETDIR + @"\" + fileName);
This loads an XML file into memory. I used the namespaces System.Query and System.XML.XLinq to perform the above "magic". One of the beauties of LINQ is just about any in-memory data structure can be queried with a single variable declaration/initialization. The one downside is you might need to use the IEnumberable interface explicitly when you load your data object. Let me know if you want more examples, and I'll try to post some.
Lay a .NET over the world