Click here to Skip to main content
11,581,210 members (65,314 online)
Click here to Skip to main content

Tagged as

How to dynamically retrieve results from a LINQ Query

, 3 Dec 2010 CPOL 7K 5
Rate this:
Please Sign up or sign in to vote.
Iterate through the results and extract using system.reflection
I looked for days for an answer and couldn't find one. Here's my solution on how to dynamically retrieve the results from a LINQ Query.

If you're stuck, I hope this helps.


'The following code will dynamically extract the
'field name and value from a LINQ IQueryable result.
'You need to:
'     Add Imports System.Reflection to your class
'     Create a LinqQuery and data context (if you're
'     looking at this code, I'm sure you know how.)
'Iterate through the result rows
For Each CurrentRow In LinqQuery
  'For each field in a row get property information
  'from the class
  For Each FieldPropertyInfo In CurrentRow.GetType.GetProperties
    'Retreive method you would call in code to get the
    'value of the field
    Dim GetMeth As System.Reflection.MethodInfo =
    'Invoke the method on the CurrentRow and retretrieve
    'the value
    Dim value As Object = GetMeth.Invoke(CurrentRow, Nothing)
    'Take a peek at the results
    Debug.Print(FieldPropertyInfo.Name & ": " & value.ToString)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) International Shipholding, Inc.
United States United States
I have a BS in Computer Science. Primary responsibilities include end user application development, back end systems integration, and database design and development. Interests include C#, VB.Net, and Any flavor of SQL.

You may also be interested in...

Comments and Discussions

GeneralGreat job James. I had the same problem. Here's a C# vers... Pin
carterlist22-Dec-10 0:44
membercarterlist22-Dec-10 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 3 Dec 2010
Article Copyright 2010 by -james
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid