Linq to Entities and Custom properties

Go To


I Have an Entity generated from a database table. Then I add a property using a partial class. This new property is the “description” part of a one to many relation. The problem is that these sets of entities have like a gazillion and four properties.

I normally use something similar to:

db.entity.Select(e => e)

But in this case, because the NEW property is not part of the table, it came empty or null.

I know I can do this:

db.entity.Select(e => new entityType { field1 = e.field1, field2 = e.field2, etc….})

But as a said before… a gazillion properties.

My question is:

There is some elegant way to just assign the value of the new property and let Linq to fill the rest?

Something like:

db.entity.Select(e => new entityType { *= e.*, newfield = e.relation.desc})

Of course, that doesn’t work, but is the idea.



2012-04-04 19:58
by epaulk


This is the approach i usually use.

   public partial class EntityName

      public string NewProp 

        get {return this.relation.Desc;}


Then in the linq query i'll do an include to make sure the related property comes along and i don't get a select n + 1 due to lazy loading.

db.entity.Select(e => e).Include(x => x.relation);
2012-04-04 20:05
by scartag
Thanks! that did the trick - epaulk 2012-04-04 21:13