simple question about linq

T

Tony Johansson

Hello!

If I have this simple linq construction

var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;

This will always give only a singe record in temp which is the result from
selct o.subfileID

If I now want to get the string value from this temp collection what is the
easiest way to do this.

Some thing like
string myID = temp.ToString();

//Tony
 
G

Göran Andersson

Tony said:
Hello!

If I have this simple linq construction

var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;

This will always give only a singe record in temp which is the result
from selct o.subfileID

If I now want to get the string value from this temp collection what is the
easiest way to do this.

Some thing like
string myID = temp.ToString();

//Tony

That will probably give you a string with the data type...

string myID = temp.First();

or perhaps

string myID = temp[0];
 
T

Tony Johansson

Hello!

This doesn' work because you get compile error.

//Tony

Göran Andersson said:
Tony said:
Hello!

If I have this simple linq construction

var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;

This will always give only a singe record in temp which is the result
from selct o.subfileID

If I now want to get the string value from this temp collection what is
the
easiest way to do this.

Some thing like
string myID = temp.ToString();

//Tony

That will probably give you a string with the data type...

string myID = temp.First();

or perhaps

string myID = temp[0];
 
F

Family Tree Mike

It should have worked fine if o.subFileID is defined as a string. You did
not say how it was defined. Perhaps: string myID = temp.First().ToString();

Tony Johansson said:
Hello!

This doesn' work because you get compile error.

//Tony

Göran Andersson said:
Tony said:
Hello!

If I have this simple linq construction

var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;

This will always give only a singe record in temp which is the result
from selct o.subfileID

If I now want to get the string value from this temp collection what is
the
easiest way to do this.

Some thing like
string myID = temp.ToString();

//Tony

That will probably give you a string with the data type...

string myID = temp.First();

or perhaps

string myID = temp[0];
 
T

Tony Johansson

Hello!

If I use ToArray() on the temp object and then use ToString() I get the
value that
I was looking for.
string subfile = temp.ToArray()[0].ToString();

Is there an easier way or this might be one of the better ways.

//Tony

Family Tree Mike said:
It should have worked fine if o.subFileID is defined as a string. You did
not say how it was defined. Perhaps: string myID =
temp.First().ToString();

Tony Johansson said:
Hello!

This doesn' work because you get compile error.

//Tony

Göran Andersson said:
Tony Johansson wrote:
Hello!

If I have this simple linq construction

var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;

This will always give only a singe record in temp which is the result
from selct o.subfileID

If I now want to get the string value from this temp collection what
is
the
easiest way to do this.

Some thing like
string myID = temp.ToString();

//Tony

That will probably give you a string with the data type...

string myID = temp.First();

or perhaps

string myID = temp[0];
 
G

Göran Andersson

Tony said:
Hello!

If I use ToArray() on the temp object and then use ToString() I get the
value that
I was looking for.
string subfile = temp.ToArray()[0].ToString();

Is there an easier way or this might be one of the better ways.

//Tony

What type is subFileID? Does it make sense to convert it to a string?

temp.First() returns the first item in the result, so it's type is the
same type as subFileID.
 
T

Tony Johansson

Hello!

In this linq query below we have the following types.
WorksheetList is type List<worksheet> and
type o.subFile is string.
so worksheet.subFile is a string

var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;


So when I do this statement I will receive the string in subfile
string subfile = temp.ToArray()[0].ToString();

So I do think is one of the better alternatives.

//Tony
 
G

Göran Andersson

Tony said:
Hello!

In this linq query below we have the following types.
WorksheetList is type List<worksheet> and
type o.subFile is string.
so worksheet.subFile is a string

But what type is the field subFileID, which is the field that you use in
the query?

Or perhaps you use the wrong field in the query, and that is the main
reason that you have trouble getting the correct data from it?
var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;


So when I do this statement I will receive the string in subfile
string subfile = temp.ToArray()[0].ToString();

So I do think is one of the better alternatives.

//Tony
 
T

Tony Johansson

Hello!

subFileID is a string
//Tony


Göran Andersson said:
Tony said:
Hello!

In this linq query below we have the following types.
WorksheetList is type List<worksheet> and
type o.subFile is string.
so worksheet.subFile is a string

But what type is the field subFileID, which is the field that you use in
the query?

Or perhaps you use the wrong field in the query, and that is the main
reason that you have trouble getting the correct data from it?
var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;


So when I do this statement I will receive the string in subfile
string subfile = temp.ToArray()[0].ToString();

So I do think is one of the better alternatives.

//Tony


Göran Andersson said:
Tony Johansson wrote:
Hello!

This doesn' work because you get compile error.

//Tony


Which compiler error?

And which of the methods did you try?
 
G

Göran Andersson

Tony said:
Hello!

subFileID is a string
//Tony

In that case you should only need:

string subfile = temp.First();

There is no reason to use the ToString method on a string.
Göran Andersson said:
Tony said:
Hello!

In this linq query below we have the following types.
WorksheetList is type List<worksheet> and
type o.subFile is string.
so worksheet.subFile is a string

But what type is the field subFileID, which is the field that you use
in the query?

Or perhaps you use the wrong field in the query, and that is the main
reason that you have trouble getting the correct data from it?
var temp = from o in myWorksheetList
where o.ID == workSheetID
select o.subFileID;


So when I do this statement I will receive the string in subfile
string subfile = temp.ToArray()[0].ToString();

So I do think is one of the better alternatives.

//Tony


Tony Johansson wrote:
Hello!

This doesn' work because you get compile error.

//Tony


Which compiler error?

And which of the methods did you try?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top