Click here to Skip to main content
15,901,373 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
PraiseRe: That moment when a lot of difficult work becomes a five minute job... Pin
RickZeeland4-Mar-18 4:35
mveRickZeeland4-Mar-18 4:35 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Sander Rossel4-Mar-18 6:23
professionalSander Rossel4-Mar-18 6:23 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
RickZeeland4-Mar-18 6:30
mveRickZeeland4-Mar-18 6:30 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
#realJSOP5-Mar-18 1:03
professional#realJSOP5-Mar-18 1:03 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Matthew Dennis4-Mar-18 6:06
sysadminMatthew Dennis4-Mar-18 6:06 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Sander Rossel4-Mar-18 6:33
professionalSander Rossel4-Mar-18 6:33 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Matthew@Home4-Mar-18 7:29
Matthew@Home4-Mar-18 7:29 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Sander Rossel5-Mar-18 6:47
professionalSander Rossel5-Mar-18 6:47 
Matthew@Home wrote:
but without seeing your existing code, I can't suggest the best way
Well, I wasn't really asking for suggestions, but I appreciate the help Smile | :)

Here's the situation:
I've got a couple of entities.
C#
public class EntityA : IMyInterface
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
    public List<DetailA> Details { get; set; }
}

public class EntityB : IMyInterface
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
    public List<DetailB> Details { get; set; }
}

public class EntityC : IMyInterface
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
    public string SomeOtherProperty { get; set; }
    public string AnotherProperty { get; set; }
    public List<DetailC> Details { get; set; }
}

// Details all pretty much look the same as well, I've got more entities...
Then I've got this model:
C#
public class SharedModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
    public List<SharedDetailModel> Details { get; set; }
}
And a query for all entities:
C#
public TModel GetModels<TModel, TEntity>()
   where TModel : SharedModel, new()
   where TEntity : class, IMyInterface, new()
{
   context.Set<TEntity>()
      .Where(...)
      .Select(e => new TModel
         {
            Id = e.Id,
            Name = e.Name,
            Active = e.Active,
            Details = e.Details.Select(...)
         }).ToList();
}
So this works for everything except SomeOtherProperty and AnotherProperty for EntityC.
So I somehow want to inject them into the selector.
C#
Expression<Func<TEntity, TModel>> baseSelect = e => new TModel
{
    Id = e.Id,
    Name = e.Name,
    Active = e.Active,
    Details = e.Details.Select(...)
};

// And in inherited class, call GetModels<MyCustomModel, EntityC>().
// Use this as parameter.
Expression<Func<TEntity, TModel>> additionalSelect = e => new MyCustomModel
{
   SomeOtherProperty = e.SomeOtherProperty,
   AnotherProperty = e.AnotherProperty
};
And then merge the two trees...
C#
MemberInitExpression baseSelectBody = (MemberInitExpression)baseSelect.Body;
MemberInitExpression additionalSelectBody = (MemberInitExpression)additionalSelect.Body;

ParameterExpression parameter = Expression.Parameter(typeof(TEntity), "e");
IEnumerable<MemberBinding> bindings = baseSelectBody.Bindings.Union(additionalSelectBody.Bindings);
MemberInitExpression init = Expression.MemberInit(Expression.New(typeof(TModel)), bindings);

// The following line does the trick and "stitches" all bindings together with the same parameter reference!
Expression newInit = new ExpressionParameterSwitcher(parameter).Visit(init);

exp = Expression.Lambda<Func<TEntity, TModel>>(newInit, parameter);
And now it's easy, just do context.Set<TEntity>().Select(exp); Big Grin | :-D

Guess I've just pretty much written a tip... Laugh | :laugh:

GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Jörgen Andersson4-Mar-18 21:32
professionalJörgen Andersson4-Mar-18 21:32 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Slow Eddie5-Mar-18 2:35
professionalSlow Eddie5-Mar-18 2:35 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Sander Rossel5-Mar-18 6:26
professionalSander Rossel5-Mar-18 6:26 
GeneralRe: That moment when a lot of difficult work becomes a five minute job... Pin
Sundance Kid15-Mar-18 0:58
Sundance Kid15-Mar-18 0:58 
GeneralA time-zone question... Pin
Kornfeld Eliyahu Peter3-Mar-18 21:25
professionalKornfeld Eliyahu Peter3-Mar-18 21:25 
JokeRe: A time-zone question... Pin
RickZeeland4-Mar-18 1:16
mveRickZeeland4-Mar-18 1:16 
GeneralRe: A time-zone question... Pin
Kornfeld Eliyahu Peter4-Mar-18 1:32
professionalKornfeld Eliyahu Peter4-Mar-18 1:32 
GeneralRe: A time-zone question... Pin
Richard MacCutchan4-Mar-18 2:06
mveRichard MacCutchan4-Mar-18 2:06 
JokeRe: A time-zone question... Pin
RickZeeland4-Mar-18 2:24
mveRickZeeland4-Mar-18 2:24 
GeneralRe: A time-zone question... Pin
Daniel Pfeffer4-Mar-18 2:28
professionalDaniel Pfeffer4-Mar-18 2:28 
JokeRe: A time-zone question... Pin
RickZeeland4-Mar-18 2:35
mveRickZeeland4-Mar-18 2:35 
GeneralRe: A time-zone question... Pin
Richard MacCutchan4-Mar-18 2:44
mveRichard MacCutchan4-Mar-18 2:44 
JokeRe: A time-zone question... Pin
RickZeeland4-Mar-18 2:48
mveRickZeeland4-Mar-18 2:48 
GeneralRe: A time-zone question... Pin
Richard MacCutchan4-Mar-18 3:20
mveRichard MacCutchan4-Mar-18 3:20 
JokeRe: A time-zone question... Pin
Sander Rossel4-Mar-18 3:33
professionalSander Rossel4-Mar-18 3:33 
GeneralRe: A time-zone question... Pin
Richard MacCutchan4-Mar-18 3:40
mveRichard MacCutchan4-Mar-18 3:40 
GeneralAnyone at MVP summit? Pin
Duncan Edwards Jones3-Mar-18 16:17
professionalDuncan Edwards Jones3-Mar-18 16:17 

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

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


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   504 votes