Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Equivalent of foreach for only one object 2

Status
Not open for further replies.

djfrear

Programmer
Jul 11, 2007
83
0
0
GB
Take this for example:

Code:
                var vData = from min in feederData.Vehicle_sizes
                            where min.Min_passengers <= Convert.ToInt32(txtPax.Text) & min.Max_passengers >= Convert.ToInt32(txtPax.Text)
                            select new
                            {
                                vType = min.Vehicle_Type
                            };

                foreach (var data in vData)
                {
                    txtSize.Text = data.vType.ToString();
                }

Now I know that there will only be one record returned into the vData object, but the only way ive been able to get the data from that object is to use a foreach statement.

What id like to know is how I can get at the same data without using a foreach as it seems a little dumb to be using foreach when I know there's only ever going to be one record returned.

Thanks in advance, Daniel.
 
Have you tried vDatat[0].vType.ToString()? It will grab the first (and only) index
 
I have, that was the first thing I tried as it seemed logical - you cant actually access the data members like this, there are simply no options.
 
replace this
Code:
foreach (var data in vData)
                {
                    txtSize.Text = data.vType.ToString();
                }

with this.

Code:
foreach (var data in vData)
                {
                    txtSize.Text = data.vType[0].ToString();
                }

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Just to clarify, the code I have given works perfectly well - I just think im missing something as there should be a way to access that single data member without putting it in a foreach, as theres only one member.
 
OOps I forgot to delete some things.

it should have been

Code:
 txtSize.Text = data.vType[0].ToString();

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
That still requires the foreach though
 
Last try

But I think it was obvious

Code:
 txtSize.Text = vData[0].vType.ToString();

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Yes unfortunately that doesnt work either, that's what I would expect to work but no go :(
 
And if you told me what the error was and other relevant information I could perhaps help you further. But then again.

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
txtSize.Text = vData[0].vType.ToString();

gives us:

"Cannot apply indexing with [] to an expression of type 'System.Linq.IQueryable<AnonymouseType#1>'"
 
Managed to solve the problem by changing the LiNQ.

As follows:

Code:
var qData = feederData.Vehicle_sizes.Single(p => p.Min_passengers <=Convert.ToInt32(txtPax.Text) & p.Max_passengers >= Convert.ToInt32(txtPax.Text));

txtSize.Text = qData.Vehicle_Type.ToString();
 
I think you went from using LINQ to using a lamda expression which is cool.

I believe if yous stuck with the LINQ method and change it to use the First() method you would have one result.

var vData = from min in feederData.Vehicle_sizes
where min.Min_passengers <= Convert.ToInt32(txtPax.Text) & min.Max_passengers >= Convert.ToInt32(txtPax.Text)
select new
{
vType = min.Vehicle_Type
}.First();



 
Yea I went from just LiNQ to LiNQ + Lambda. I'll take a look at that first method though, for future reference.
 
Probably vData is implementing IEnumerable but not actually an array or list, that's why you can use it with foreach and not with an indexer.

(MSDN says that a type does not have to implement IEnumerable to be compatible with foreach, but should have the required methods, eg. GetEnumerator, to work with foreach).

Try getting the default enumerator and get the element via the Current property.

Something like...

IEnumerator enumerator = vData.GetEnumerator();
object obj = enumerator.Current;

I know nothing about Linq, but I hope this helps. [wink]
 
Ah, nice. Thanks phinoppix - vData is implementing IEnumerable but my knowledge of how to actually use that is null so thanks for the information.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top