Results 1 to 5 of 5

Thread: [RESOLVED] LINQ Problem probably simple please help.

  1. #1

    Thread Starter
    New Member lilsammy's Avatar
    Join Date
    Apr 2011
    Location
    Peterhead, Scotland
    Posts
    9

    Resolved [RESOLVED] LINQ Problem probably simple please help.

    OK I have been trying to work this out for a while now I am very much learning vb.net and LINQ is part of what we are being told to use for our coursework and it makes sense to use it but its just not working for me. I have added my database.mdf to data sources and added linq to sql and added all the tables in the database as data source objects. Anyway I think I did something wrong but I can't see what it is all I want to do is retrieve the company names at this stage and I can build on that myself but I can't get it to work.

    This is what my VS looks like hope someone can spot something I didn't.



    P.S. I am very stupid yes...

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: LINQ Problem probably simple please help.

    You can combine LINQ2SQL with datasets, but usually you don't. You generally just need to create a datacontext and pull your data from that. Usually the datacontext will be named based on what you named the dbml file. So in yours it looks like it is DataClass.dbml, so there SHOULD be a DataClassDataContext class you can create an instance of.

    Code:
            Using myDataContext As New DataClassDataContext
                Dim comp = From C In myDataContext.Companies
                           Select C.Name Distinct
    
                Return comp
            End Using

  3. #3

    Thread Starter
    New Member lilsammy's Avatar
    Join Date
    Apr 2011
    Location
    Peterhead, Scotland
    Posts
    9

    Re: LINQ Problem probably simple please help.

    OK I half understand what you mean is there anywhere I can learn more about what you are trying to explain? because I am still not fully with you I don't want to take any more of your time up if there is already something out there to explain what you mean.

    This is the program I am working on just now if anyone is interested its very unfinished the final program will need to query the database and display the results. What I am stuck on just now is getting a basic starting point with the LINQ/Sql

    Stuido 2008.zip

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: LINQ Problem probably simple please help.

    returning comp.ToString is going to return the SQL syntax string that LINQ is using to query your database, which of course is not what you want. To see it working properly, change your function to this:

    Code:
        Public Function GetCompNames()
            Using myDataContext As New DataClassDataContext
                Dim comp = From C In myDataContext.Companies _
                           Select C.Name Distinct
                Return comp.ToArray  
          End Using
        End Function
    and change your form_load in your GUI form to this:

    Code:
        Private Sub GUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Populate the filter boxes
            Populate.ResetFilters()
            Populate.PopulateLists()
    
            TxtDebug.Text = String.Join(",", Sql.GetCompNames)
    
        End Sub

  5. #5

    Thread Starter
    New Member lilsammy's Avatar
    Join Date
    Apr 2011
    Location
    Peterhead, Scotland
    Posts
    9

    Re: LINQ Problem probably simple please help.

    Thanks for taking the time to look at that man, I will check it out in the morning and let you know how it goes just checking the thread on my laptop in bed before I go sleep as I couldn't stop thinking about it

    EDIT: Just tested it thanks a lot got me moving again
    Last edited by lilsammy; Apr 16th, 2011 at 03:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width