Solved

LINQ to EF abstract column names

Posted on 2010-08-31
6
512 Views
Last Modified: 2013-11-11
Hi,
I was wondering whether it is possible to to do something like this:

            var x = (from d in DBContext.SOMETABLE
                          select d).Single();

            x.column("SOMECOLUMN") = "abc";
            DBContext.SaveChanges();

ie. I want to be able to access the column names abstractly, this is because I conditionally need to updated different columns and begin able to do the above will make the code far shorter and easier to read.
0
Comment
Question by:Alw1n
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:with
ID: 33566105
X is basically just a regular class, so you can use regular old Reflection to set a class's property by name.  Since the code-generated class EF makes should have the same column names in the database by default, it should work out well enough (psuedo-code, ymmv but should be pretty close)

PropertyInfo columnproperty = SOMETABLE.GetType().GetProperty("SOMECOLUMN");

var x = (from d in DBContext.SOMETABLE select d).Single();

columnproperty.SetValue(x, "abc", null);
0
 

Author Comment

by:Alw1n
ID: 33566465
Thanks, it looks promising but I can't access my column with:

PropertyInfo columnproperty = SOMETABLE.GetType().GetProperty("SOMECOLUMN");

It appears that the SOMECOLUMN property is buried somewhere deeper in the entity object as the code above produces a result of null.
0
 
LVL 3

Accepted Solution

by:
with earned 500 total points
ID: 33566616
Here's a more concrete example; I actually tested this so it should work.  This is using my context, you'll have to replace the names with yours:

etsykitEntities context = new etsykitEntities();

//Get 1 item to deal with.  I'll just make one, although I could have selected one
Item oneItem = ctx.Items.CreateObject();

//Notice the difference here; getting type of Item, not Item*s*
PropertyInfo p = typeof(Item).GetProperty("Title");

//Setting the "Title" field of this entity using reflection:
p.SetValue(oneItem, "new title", null);

The problem with my initial example was that I had you use the *object set* type, but that doesn't have any database-specific properties on it.  You want to use the *entity* type.  Hope this makes more sense.
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 

Author Closing Comment

by:Alw1n
ID: 33568114
Worked perfectly, thank you
0
 
LVL 21

Expert Comment

by:naspinski
ID: 33568604
I wrote an article about this a while back: http://www.naspinski.net/post/Edit-an-Object-Property-Value-Dynamically-at-Runtime.aspx

Also, I included a simple extension to do this without any code in m my utilities library: http://naspinski.codeplex.com/wikipage?title=DynamicProperty&referringTitle=Documentation
0
 

Author Comment

by:Alw1n
ID: 33574161
Thanks Naspinski, that is a really cool idea to use this as an extension!
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question