Results 1 to 8 of 8

Thread: Simple Linq Query

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Simple Linq Query

    Hey,

    Ok I'm working on getting to Grips with some LINQ for Database access. I've setup my DBML and database connection. I have a table called PageSettings and would like to extract all rows and then iterate through them. Can anyone provide me with a sample?

    Table: Page Settings

    Code:
    DBDataContext DBData = new DBDataContext();
    Any help appriciated

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Simple Linq Query

    Code:
    using(DBDataContext DBData = new DBDataContext())
    {
        var PageSettings = from pagesetting in DBData.PageSettings
                           select pagesetting
        foreach(var PageSetting in PageSettings)
        {
            //Do Stuff
        }
    }
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Linq Query

    Of course, if you want use all rows then you don't actually need any LINQ code:
    csharp Code:
    1. using (DBDataContext dbData = new DBDataContext())
    2. {
    3.     foreach (var pageSetting in dbData.PageSettings)
    4.     {
    5.         MessageBox.Show(pageSetting.Name);
    6.     }
    7. }
    It's only if you wanted to filter or project that you'd need a LINQ query because PageSettings is already a source of all records.

    Also, if you don't already have a significant investment in LINQ to SQL I'd suggest that you forget it and use the Entity Framework. I could be wrong but, as far as I'm aware, LINQ to SQL doesn't offer any advantages over the Entity Framework while the opposite is not the case. Since the creation of the Entity Framework there have been fears about the future of LINQ to SQL. Microsoft say that it won't disappear but they're unlikely to look to develop it further and I wouldn't be surprised if it does go the way of the Dodo some time soon because it's basically redundant.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Simple Linq Query

    Thanks Guys,

    jmcilhinney, Can you provide any reference links for the Entity Framework, if what you are saying is correct that's big news!

    Pino

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Simple Linq Query

    jm is right. LINQ to SQL is on its death bed.
    I usually use only LINQ to XML or LINQ to Objects and never felt the need to use LINQ to SQL, though it might be worth learning if ms decides to develop it furthur.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Linq Query

    I haven't read too much about the Entity Framework as far as actually how to use it. I watched a dnrTV episode and then experimented. There is that whole "vote of no confidence" thing but, if you're thinking of using LINQ to SQL then that shouldn't be a worry because it doesn't address any of those issues anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Simple Linq Query

    What I'm really doing is looking for a replacement to our current DAL (Which is SubSonic - http://subsonicproject.com/)

    I've been doing some reading (Wokas New Book) and it mentions Linq to Entities. It would be interesting to see it as a comparable to SubSonic which has served us well.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Simple Linq Query

    The Entity Framework basically provides a class wrapper over the dataset so that you feel at ease working with properties/methods as you would do with normal classes.
    Have a look at it here to begin:
    http://msdn.microsoft.com/en-us/libr...27(VS.80).aspx

    The article also shows difference between the Entity Framework and LINQ to SQL approaches and how they compare against each other.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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