|
-
Jul 27th, 2008, 10:32 PM
#1
Thread Starter
Addicted Member
[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)
-
Jul 27th, 2008, 11:09 PM
#2
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.
-
Jul 27th, 2008, 11:39 PM
#3
Thread Starter
Addicted Member
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?
-
Jul 27th, 2008, 11:44 PM
#4
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.
-
Jul 28th, 2008, 12:27 AM
#5
Thread Starter
Addicted Member
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))
-
Jul 28th, 2008, 12:30 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|