Results 1 to 6 of 6

Thread: [RESOLVED] [.Net 2008] LINQ to SQL class question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Location
    Miami, FL USA
    Posts
    171

    Resolved [RESOLVED] [.Net 2008] LINQ to SQL class question

    Hey everyone, I am trying to accomplish something like the example below. I know you can accomplish the same task in a different way (as shown in example 2) but this will not work in my case. I am collecting the column name at runtime so I cannot strongly type (if that is the correct terminology) the column name. Any help would be greatly appreciated.

    Example 1 (does not work)
    Code:
        Dim MyCustomer = From Customer In DB.Customers _
        Where Customer.ID = 1 _
        Select Customer
    
        MsgBox(MyCustomer.First.Item("Name").ToString) <----- No "Item" property
    Example 2 (does work but not what I need)
    Code:
        Dim MyCustomer = From Customer In DB.Customers _
        Where Customer.ID = 1 _
        Select Customer
    
        MsgBox(MyCustomer.First.Name)

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

    Re: [.Net 2008] LINQ to SQL class question

    The objects returned from your query are just like any other objects. You can't specify a property name using a string for any other types so you can't for LINQ anonymous types either. If you want to use a string to identify a property then you need to use reflection via the PropertyInfo class.
    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
    Addicted Member
    Join Date
    Aug 2007
    Location
    Miami, FL USA
    Posts
    171

    Re: [.Net 2008] LINQ to SQL class question

    I know that I should know what you’re talking about but I'm a little lost. Could you possibly provide a sample or point me in the direction of some info on this subject?

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

    Re: [.Net 2008] LINQ to SQL class question

    Ah, you must have got one of those versions of Visual Studio that doesn't have a Help menu. If it was me, I'd just select Help -> Index from the main menu and then type propertyinfo into the box. I guess I'm just lucky.
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Location
    Miami, FL USA
    Posts
    171

    Re: [.Net 2008] LINQ to SQL class question

    Thanks for your help. To my surprise, there is a help menu! I ended up with this which works great.

    Code:
        Dim MyCustomer = From Customer In DB.Customers _
        Where Customer.ID = 1 _
        Select Customer
    
        Dim pInfo As System.Reflection.PropertyInfo = _
        GetType(Customer).GetProperty("Name")
    
        MsgBox(pInfo.GetValue(MyCustomer.First, Nothing))

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

    Re: [RESOLVED] [.Net 2008] LINQ to SQL class question

    See what you can do when you make use of the resources provided? This is why I get grouchy at people. Many people who post here are quite capable of answering many of their own questions but just don't look in what should be the obvious place. I commend you on working out the problem for yourself. Hopefully you'll benefit by being able to solve more of your own problems in future by consulting the documentation. You'll get answers faster and you'll learn more in the process. It's how I learned. Of course, for the questions that are too curly for the doco, we'll be here.
    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