473,385 Members | 1,898 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,385 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 2040
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
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.