From: "Steve D" <nospam_steved94@comcast.net>
Subject: Re: Lambda expressions? LINQ?
Date: Fri, 18 Sep 2009 19:33:51 -0700
Date: 2009-09-18T19:33:51-07:00 [thread overview]
Message-ID: <mqXsm.11303$944.819@newsfe09.iad> (raw)
In-Reply-To: <9b330aae-2c83-4d1c-995b-192425cd1c52@m11g2000vbl.googlegroups.com>
>"sjw" <simon.j.wright@mac.com> wrote in message
>news:9b330aae-2c83-4d1c-995b-192425cd1c52@m11g2000vbl.googlegroups.com...
>On Sep 18, 2:59 am, "Steve D" <nospam_steve...@comcast.net> wrote:
>> "Jeffrey R. Carter" <spam.jrcarter....@spam.acm.org> wrote in
>> messagenews:h8qf72$jv2$2@news.tornevall.net...
>>
>> > Steve D wrote:
>>
>> >> Using LINQ and the .NET framework I was able to query an XML document
>> >> for
>> >> all elements with a given element name, select an attribute with a
>>> >> specific name, split the content of the attribute (a comma separated
>>> >> list
>> >> of strings) into it's individual strings and return an array of unique
>> >> elements... in one line of source code. ... Wow!
>>
>> > Sounds like a write-only language to me.
>>
>> Oh, so you are familiar with LINQ and find that you cannot make queries
>> that
>> are readable?
>> My experience is not the same.
>
>It would certainly be interesting to see this one-liner (or even an
>approximation to it!).
As Martin guessed it is one statement spread over a few lines.
string[] names = xDoc.Descendants("Layer0Message")
.SelectMany(node =>
node.Attribute("To").Value.Split(' ',',').Where( st => st.Length > 0 ))
.Distinct().OrderBy(n=>n).ToArray();
There is an alternate way of formatting the query... more like SQL, but I
prefer the version that uses the dot notation.
Here's a small chunk of the XML data it is reading:
<File>
<!-- Message #1 -->
<NewMessage>
<Time>10:08:19.812</Time>
<Layer0Message Type="2000" Name="MAL_RAW_ENCODER" From="encoder" To="
sequencer, turnset" OriginalBytes="12" SavedBytes="12" DataFormat="INT8 HEX"
/>
<Data>63 BF F4 C0 C7 3C 02 00 C3 4F 4F 14 </Data>
</NewMessage>
<!-- Message #2 -->
<NewMessage>
<Time>10:08:19.812</Time>
<Layer0Message Type="2210" Name="ROTATION_MEASUREMENT" From="rda" To="
turnset" OriginalBytes="6" SavedBytes="6" DataFormat="INT8 HEX" />
<Data>88 3D 5A C2 32 BD </Data>
</NewMessage>
Oh, and by the way. LINQ isn't just for XML. You can do queries on arrays,
lists, etc. For example if you have an array of records you can do a query
to select all of the records with a field that has a particular value. It's
cool.
I just wish my favoriate language had this feature.
Regards,
Steve
next prev parent reply other threads:[~2009-09-19 2:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-16 2:23 Lambda expressions? LINQ? Steve D
2009-09-16 8:03 ` Georg Bauhaus
2009-09-16 10:46 ` Jeffrey R. Carter
2009-09-18 1:59 ` Steve D
2009-09-18 5:23 ` sjw
2009-09-19 2:33 ` Steve D [this message]
2009-09-19 6:50 ` Martin
2009-09-19 14:35 ` Brad Moore
2009-09-19 16:38 ` Martin
2009-09-18 13:42 ` Jeffrey R. Carter
2009-09-18 15:45 ` Martin
2009-09-18 7:58 ` Martin
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox