473,396 Members | 2,099 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,396 software developers and data experts.

Simple LINQ problem

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)

Sep 25 '08 #1
3 926
James Page wrote:
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
Sep 25 '08 #2
James Page wrote:
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()
Label1.text = name


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 25 '08 #3
Thanks guys.

Do you have any links to some tutorials for LINQ other than the ones on
asp.net or MSDN?
Sep 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Veeneman | last post by:
I've been hearing a lot about LINQ in connection with Orcas, the next release of VS.NET. Micorosoft touts LINQ as the Next Big Breakthrough, but it looks to me like further muddying of application...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
22
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast....
6
by: Dmitry Perets | last post by:
Hello, I am trying to work with MS SQL Server 7 from the release version of Visual Studio 2008 + LINQ to SQL. And the problem is that the LINQ to SQL designer doesn't accept my tables saying...
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
4
by: Jacek Jurkowski | last post by:
Why is it so slow? I really like that queries but using DataReader i have done my task's much more faster than ising LINQ...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.