Simple LINQ problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SmFtZXMgUGFnZQ==?=

    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 myTableDataCont ext
    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)

  • Lloyd Sheen

    #2
    Re: Simple LINQ problem

    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 myTableDataCont ext
    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 myTableDataCont ext
    Dim name as string = (From n In db.myTable Where n.Id = 1 Select n).single

    Label1.text = name

    LS

    Comment

    • Martin Honnen

      #3
      Re: Simple LINQ problem

      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 myTableDataCont ext
      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).First OrDefault()
      Label1.text = name


      --

      Martin Honnen --- MVP XML

      Comment

      • =?Utf-8?B?SmFtZXMgUGFnZQ==?=

        #4
        Re: Simple LINQ problem

        Thanks guys.

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

        Comment

        Working...