LINQ to SQL Forum?

Last post 12-05-2007 9:07 PM by Allen Chen – MSFT. 3 replies.

Sort Posts:

  • LINQ to SQL Forum?

    12-04-2007, 5:31 PM
    • Loading...
    • cyzfitz
    • Joined on 02-05-2004, 11:44 AM
    • Posts 26

    Is there an official forum to discuss LINQ? Is this the right place?

    I've decided to dive in and play around with LINQ and I haven't been able to find much documentation as of yet and I'm coming up with several questions. I'm especially interested in using LINQ in BLL classes and what the best practices would be.

    Thanks,
    Tim

  • Re: LINQ to SQL Forum?

    12-05-2007, 1:30 PM

    I use LINQ in one of my WPF applications.  I don't even need to make BLL objects as LINQ does this for me based on my tables.

    What you do is add a LINQ to SQL file to your project.  Open a new connection to your database in the server explorer window.  Drag and drop your tables into the dbml file.  It will create all of your objects for each table with properties and proper types.  If you do your relationships in the database properly LINQ will even build the proper relationships in your objects.  For example, your Entries table has a foreign key pointing to the Accounts table's AccountID primary key.  you will see the relationships once you drop the 2 tables into the dbml visual designer.  LINQ also builds your databasecontext object so that you can instantiate the database and query it.  Make sure you are "using System.LINQ;" at the top of the cs file and you will be able to do anything in LINQ.

     

    var q = from a in db.Accounts

              where a.AccountID = new Guid(AccountID)

              Select a;

     

    q.Single().<any field shows here>

    Or if you have a bunch of records returned:

    foreach (var result in q)

    {

           result.<any field shows here>

    }

     

     

    Hope this helps.

    http://weblogs.asp.net/scottgu/archive/tags/LINQ/default.aspx



     

  • Re: LINQ to SQL Forum?

    12-05-2007, 4:18 PM
    • Loading...
    • cyzfitz
    • Joined on 02-05-2004, 11:44 AM
    • Posts 26

    Thanks.

    I have an ASP.NET app. It currently uses a DAL and BLL. For practice, I'm trying to convert this to use LINQ for SQL. I understand that LINQ basically acts as the DAL, but now I'm trying to build the BLL. I build a class that has a function to return data. However, I am uncertain as to what datatype the function should be when returning the object. I want to be able to use the returned object as datasource for UI web controls (i.e. gridview) and also be able to iterate thru it like I could with a strongly typed data table.

     

    Namespace MyApp
    
        Public Class Employee
            Public Function GetEmployees() As ?????
                Dim db As New MyAppDataContext
                Return From e In db.Employees _
                          Order By e.LastName, e.FirstName _
                          Select e
            End Function
        End Class
    
    End Namespace

    In looking thru various posts on MSDN, here, etc that declare the functions as IQueryable or IEnumerable. I'm not exactly sure which is best or even how to do it. Could someone post and example?

    Thanks,
    Tim

  • Re: LINQ to SQL Forum?

    12-05-2007, 9:07 PM
    Answer

    Hi:

      IQueryable inherits IEnumerable. So it's definitly ok to return IEnumerable. In your case though, since the returned type is actually IQueryable it's more suitable to return IQueryable instead of IEnumerable. Just assign the return value to var and retrieve data as normal.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter