Simple LINQ problem

J

James Page

Hi all

Having a mental block with linq!
I have a simple aspx page with a button and a label. When the button is
clicked I want the label to display the result of a simple query:
i.e. Select name from MyTable where id = 1
This should return ‘peter’ - simplicity using sql

I'm trying to do this with linq and getting nowhere (converting to string
error)
here's what I've got:

Button click event:

Dim db as New myTableDataContext
Dim name = From n In db.myTable Where n.Id = 1 Select n

Label1.text = name

Where am I going wrong?
Any pointers or tutorials would be great (vb.net)
 
L

Lloyd Sheen

James said:
Hi all

Having a mental block with linq!
I have a simple aspx page with a button and a label. When the button is
clicked I want the label to display the result of a simple query:
i.e. Select name from MyTable where id = 1
This should return ‘peter’ - simplicity using sql

I'm trying to do this with linq and getting nowhere (converting to string
error)
here's what I've got:

Button click event:

Dim db as New myTableDataContext
Dim name = From n In db.myTable Where n.Id = 1 Select n

Label1.text = name

Where am I going wrong?
Any pointers or tutorials would be great (vb.net)

James,

Your query will return a collection of strings. The query has no way
to know that only one string will be returned. So when you attempt to
assign "name" to the text property it does not know what to do.

If Id is the key of the table then change your code to the following
(just typed in so there may be problems but you will get the point)


Dim db as New myTableDataContext
Dim name as string = (From n In db.myTable Where n.Id = 1 Select n).single

Label1.text = name

LS
 
M

Martin Honnen

James said:
I have a simple aspx page with a button and a label. When the button is
clicked I want the label to display the result of a simple query:
i.e. Select name from MyTable where id = 1
This should return ‘peter’ - simplicity using sql

I'm trying to do this with linq and getting nowhere (converting to string
error)
here's what I've got:

Button click event:

Dim db as New myTableDataContext
Dim name = From n In db.myTable Where n.Id = 1 Select n

Dim name As String = (From row In db.myTable Where row.Id = 1 Select
row.Name).FirstOrDefault()
 
J

James Page

Thanks guys.

Do you have any links to some tutorials for LINQ other than the ones on
asp.net or MSDN?
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top