473,224 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Getting information from IEnumerable

At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?

--
Regards
Konrad Viltersten
Jul 22 '08 #1
9 2032
On Tue, 22 Jul 2008 14:31:43 -0700, K Viltersten <tm**@viltersten.com>
wrote:
At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?
When you write "my IEnumerable<XElement>" does that mean that you're the
author of the class that returns the IEnumerable?

If so, yes...you should easily be able to add an indexer. See:
http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
Jul 22 '08 #2
K Viltersten wrote:
At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?
Yes. Just stuff it in a List<XElement>. There
is a List<constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.

Arne
Jul 22 '08 #3
On Jul 23, 1:31*am, "K Viltersten" <t...@viltersten.comwrote:
At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?
If you just need to get an element at a specific index once, use
ElementAt() method (but you should be aware that it does a linear scan
unless the object it's called is actually an IList<T- then it uses
the indexer). To find an element by name, use Elements() method which
takes an XName argument; if there is precisely one such element, use
First() method to get the first value from the IEnumerable returned by
Elements().
Jul 23 '08 #4
>At this point, i usually use a foreach-loop
>to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?

Yes. Just stuff it in a List<XElement>. There
is a List<constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.
Great info! Thanks to all!

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 23 '08 #5
>At this point, i usually use a foreach-loop
>to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?

Yes. Just stuff it in a List<XElement>. There
is a List<constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.
As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto a
List<XElement>... Are you sure it's possible with
no explicit casting involved?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 23 '08 #6
K Viltersten wrote:
>>At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?

Yes. Just stuff it in a List<XElement>. There
is a List<constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.

As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto a
List<XElement>... Are you sure it's possible with
no explicit casting involved?
No casting.

But you do need to call the List<Tconstructor that accepts an
IEnumerable<T>.

e.g.
List<TtheList = new List(theEnumerable);

and not

List<TtheList = theEnumerable;
Jul 23 '08 #7
K Viltersten wrote:
>>At this point, i usually use a foreach-loop
to run through all the XElement's in my
IEnumerable<XElement>. I noticed, i'd like
to address the elements using brackets and
an index (or like a dictionary, with the
tag name as a key).

Is it easily doable? If so, how?
Yes. Just stuff it in a List<XElement>. There
is a List<constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.

As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto a
List<XElement>... Are you sure it's possible with
no explicit casting involved?
"stuff it in" = use it as argument for constructor

assignment does not really stuff much

Arne
Jul 24 '08 #8
On Jul 23, 10:14*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto a
List<XElement>... Are you sure it's possible with
no explicit casting involved?

No casting.

But you do need to call the List<Tconstructor that accepts an
IEnumerable<T>.

e.g.

List<TtheList = new List(theEnumerable);
Or, a tiny bit shorter with LINQ:

var theList = theEnumerable.ToList();
Jul 24 '08 #9
>>Yes. Just stuff it in a List<XElement>. There
>>is a List<constructor that takes an IEnumerable<>
as argument.

And if it comes from LINQ for XML there is also
ToList in that.

As far i can see, DotNet complains when i try to
shove the obtained IEnumerable<XElementinto a
List<XElement>... Are you sure it's possible with
no explicit casting involved?

"stuff it in" = use it as argument for constructor
assignment does not really stuff much
I see you, very correctly and clearly so,
mention the use of constructor, when talking
about stuffing in. I must have missed it.
Please accept my appology.

I though by stuffing in you ment:
int stuffee = 3;
double stuffer = stuffee;
Well, i'll be off correcting the error.
Thanks to all of you guys!

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 24 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
3
by: KH | last post by:
I can't seem to figure this one out... I've searched MSDN and Goog, and made my best guesses to no avail,, so help would be much appreciated! public ref class T sealed : public...
5
by: Tin Gherdanarra | last post by:
Dear mpdls, here is a simple example of an IEnumerable that generates integers: It works, but I have only a vague idea of what's going on. I understand that /yield/ wraps the humble integer...
2
by: =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post by:
When creating multiple iterators, the original is defined as returning IEnumerator, ie public IEnumerator GetEnumerator() { yield x; ...} whereas the additional ones are defined as returning...
2
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for...
4
by: Ronald S. Cook | last post by:
Thanks guys for all the guidance on this so far. In the below, for _IEnumerable2, I'm just trying to hard-code one record with "(Please Select)" for a value. I can do that easily in SQL but...
12
by: gnewsgroup | last post by:
I've read the msdn doc about IEnumerable. It seems to me that IEnumerable objects are essentially wrapped-up arrays. It simply gives us the foreach convenience. Is this correct?
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
1
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
Something I found surprising this week involves the IEnumerable<Tinterface. I have a class that I wrote a couple of years ago which implements the IEnumerable interface. This week I realized it...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.