Rockford .Net User Group

The premier .Net User Group in the Northern Illinois Area
Welcome to Rockford .Net User Group Sign in | Join | Help
in Search

LINQ

Last post 09-21-2007, 12:02 PM by Ross Albertson. 4 replies.
Sort Posts: Previous Next
  •  09-01-2007, 11:51 AM 65

    LINQ

    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
  •  09-17-2007, 10:38 AM 67 in reply to 65

    Re: LINQ

    Ross,

     

    Do you think you would be willing to give a presentation on this at an upcoming meeting? 

     

    Chris 

  •  09-17-2007, 4:01 PM 69 in reply to 67

    Re: LINQ

    Chris,

    If I do, it will be primitive, since I don't have a laptop or experience with Powerpoint. My current project is a console program written in VB.NET using both LINQ and traditional methods. I am using Visual Studio 2008 Beta 1 for the purpose. If I had a partner to help with the shortcomings I mentioned, I would be willing to show what I've learned. What might be easier would be for me to write a report and distribute it at a later meeting.

    Ross

     


    Lay a .NET over the world
  •  09-19-2007, 10:57 AM 70 in reply to 67

    Re: LINQ

    Chris,

    I might be able to present LINQ in the future. I plan to learn PowerPoint and I might have access to a laptop. I might need help hooking up the projector. You might be interested to know that I bought a book on LINQ and I recently installed an alpha of LINQ-to-XSD. I haven't tested the alpha yet, but I believe it allows me to "compile" Microsoft-style XML schemas into classes, which in turn allows typed queries of XML files. I'll let you know if I'm right. Lastly, I think I read that installing Framework 3.5 allows you to use LINQ with ASP.NET.

    Ross

     

     


    Lay a .NET over the world
  •  09-21-2007, 12:02 PM 71 in reply to 67

    Re: LINQ

    Chris,

    I've been playing with PowerPoint and thinking about the best way to structure a presentation of LINQ. I have a couple of questions:

    1. How long should I make it?

    2. Should I emphasize versatility or power?

    Ross 

     


    Lay a .NET over the world
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems