Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2007

Tip: Looking for answers? Try searching our database.

Use LINQ and get field values by name

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anton Bar - 03 Dec 2007 09:13 GMT
Hi all,

I have the following problem. I have a database table that I want to read
using LINQ and being able to access field values by name, like this:

var languages = from lg in context.SystemLanguages select lg;
foreach (SystemLanguage lang in languages)
{
    // now I'd like to do smth like this:
    string name = lang.Field(sFieldName);
    .........    
}

The problem is that the "sFieldName" string is built dynamically, so I
cannot use the predefined strongly typed properties!!!

Any bright ideas?

Thanks in advance,
Anton.
Frans Bouma [C# MVP] - 03 Dec 2007 10:18 GMT
> Hi all,
>
[quoted text clipped - 14 lines]
>
> Any bright ideas?

    TypeDescriptor.GetProperties(typeof(SystemLanguage))

    this will get you property descriptors. You can use a property
descriptor with 'lang' to obtain a property's value by calling GetValue
on the property descriptor object.

    So you do something like:
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(SsytemLanguage));
foreach(SystemLanguage lang in languages)
{
    string name = (string)properties[sFieldName].GetValue(lang);
}

    It's not as fast as reading normal properties, but it shows you what
the possibilities are. :)

        FB

Signature

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.