Results 1 to 4 of 4

Thread: [RESOLVED] LInq question

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Resolved [RESOLVED] LInq question

    Hi there,

    I am just starting to use linq, I have created this little bit to pull information out of a datatable.

    Code:
    var brideFirstName = from r in eventContacts.Tables[0].AsEnumerable() where(r.Field<string>("ContactType") == "bride")
                                                select(r.Field<string>("FirstName"));
    BrideFirstNameLabel.Text = brideFirstName.First();
    first of all - is that the most efficient way to get a single piece of data from a datatable?

    Second question - what if I wanted say three bits of data, like city state and postal code?

    I have read several tutorials but am not that familiar with it yet so any human advice would be great!

    Thanks...
    Anti DUPLO machine!!!

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

    Re: LInq question

    That looks OK to me, although you really should format the code properly. When people write code sloppily they more easily get confused as to what it does, not to mention that it makes it harder for others to understand. Properly formatting code is easy to do and helps everyone:
    Code:
    var brideFirstName = from r in eventContacts.Tables[0].AsEnumerable()
                         where r.Field<string>("ContactType") == "bride"
                         select r.Field<string>("FirstName");
    With regards to the last line, you would call FirstOrDefault if you weren't 100&#37; sure that a matching row will be found. If you are 100% sure then First is the correct option.

    With regards to the last question, you would create an anonymous type in the select clause, e.g.
    Code:
    var brideFullName = (from r in eventContacts.Tables[0].AsEnumerable()
                         where r.Field<string>("ContactType") == "bride"
                         select new
                         {
                             FirstName = r.Field<string>("FirstName"),
                             MiddleName = r.Field<string>("MiddleName"),
                             LastName = r.Field<string>("LastName")
                         }).First();
    The new { ... } part creates a new object with the specified properties with the specified values. It's called an anonymous type because it's not an existing type with a name, hence there's no type name after the 'new' keyword. The type is created ad hoc and only exists for the life of the instance(s) created in that code. You can access the property values just as you would property values of any other object:
    csharp Code:
    1. BrideFirstNameLabel.Text = brideFullName.FirstName;
    2. BrideMiddleNameLabel.Text = brideFullName.MiddleName;
    3. BrideLastNameLabel.Text = brideFullName.LastName;
    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

  3. #3

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: LInq question

    On your first point about sloppy code - Yeah, I translated that query from one using "uhm, extensions... I think"

    With regards to your second question I was just reading O'Riellys .net 3.5 book (the one that is an overview of all it) last night after I posted this and I just read about anonymous types - we'll, a practical use of them.

    thanks a lot - that was helpful.
    Anti DUPLO machine!!!

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

    Re: LInq question

    Quote Originally Posted by r0k3t View Post
    With regards to your second question I was just reading O'Riellys .net 3.5 book (the one that is an overview of all it) last night after I posted this and I just read about anonymous types - we'll, a practical use of them.
    Not only is it a practical use, it's the very reason that anonymous types were created in the first place. While extension methods, lambda expressions and anonymous types all come in handy elsewhere too, they were created specifically to support LINQ.
    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

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