Simple LINQ problem

  • Thread starter Thread starter James Page
  • Start date Start date
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)
 
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
 
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()
 
Thanks guys.

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