|
You could do something with the Join method but I would derive an new model from TModel.
There are a number of ways to do what you want, but without seeing your existing code, I can't suggest the best way. You really want this to only make one call to the database, so getting the right projection in the first place is important.
|
|
|
|
|
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
Here's the situation:
I've got a couple of entities.
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; }
}
Then I've got this model:
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:
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.
Expression<Func<TEntity, TModel>> baseSelect = e => new TModel
{
Id = e.Id,
Name = e.Name,
Active = e.Active,
Details = e.Details.Select(...)
};
Expression<Func<TEntity, TModel>> additionalSelect = e => new MyCustomModel
{
SomeOtherProperty = e.SomeOtherProperty,
AnotherProperty = e.AnotherProperty
}; And then merge the two trees...
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);
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);
Guess I've just pretty much written a tip...
|
|
|
|
|
Bookmarked.
This might come in handy if i ever rebuild my caching class.
I just wish you could've given the thread a more descriptive name, next time I see it among the bookmarks I won't have a clue what it is about.
|
|
|
|
|
|
That's also very familiar
|
|
|
|
|
Yip.. something that should be fast takes forever. Uhg
And then having to explain to management why...
|
|
|
|
|
It seems to me that the 'The Machine Learning and Artificial Intelligence Challenge' closed 24 hours earlier...
@chris-maunder?
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
modified 4-Mar-18 3:59am.
|
|
|
|
|
I generally refer to those people in other time zones as 'Time Bandits'
|
|
|
|
|
I hope you are aware that there are millions of 'time bandits' all around you?
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
We refer to them as foreigners.
|
|
|
|
|
Quote: You're as cold as ice
You're willing to sacrifice our love
You never take advice
Someday you'll pay the price, I know
|
|
|
|
|
That song takes me back...
It was on the first cassette that I bought for my brand-new Walkman. How time flies.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Foreigner was in the 'hitparade' in Holland for weeks, must have been in 1977, and I still hear it regularly on the radio ...
So don't accuse us of having anything against Foreigner
|
|
|
|
|
Sounds like me ...
|
|
|
|
|
Now now, cheer up, you can't be that bad, remember that you were never banned from CodeProject !
Did that cheer you up a little
|
|
|
|
|
|
Speaking of which, the world is going to hell and it's all the fault of those darn foreigners!
There are 7 billion people on earth and do you know how many of them are foreigners!?
|
|
|
|
|
Yup, too many.
|
|
|
|
|
Any fellow codeproject persons here fancy getting together for a coffee* some stage during the summit?
*By which I mean beer. The coffee here is not great.
|
|
|
|
|
Well,
I walked around at one of the last MVP summits a few years ago (sampling the food) but the only person I could recognize was Nish. I spoke with him briefly and left 15 minutes later...
What kind of place are you looking for? A grimy pub atmosphere or something more fancy? There's actually not much to do in Redmond... you might be better off driving off campus down to Seattle.
Best Wishes,
-David Delaune
|
|
|
|
|
I would like to, but as it's not exactly in the neighborhood for me I have to decline I'm afraid.
Now if you had proposed 'in an Irish Pub' then ...
|
|
|
|
|
I think I will go to bed early, so no reactions on posts for at least 8 hours (my phone is off too)
And for those in other time zones (aka Time Bandits) Goodmorning, Goodevening etc.
|
|
|
|
|
Night-night, sleep tight; don't let the bed-bugs bite.
(And if they do, use dynamite)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Thanks for the dynamite tip, caused a bit of a mess
But now thanks to a hearty breakfast I feel much better, we did a bit of British/French fusion cooking:
in a small baking mold put croissant dough, a slice of bacon, cheese and an egg and bake that in the oven or air fryer for 15 minutes at about 200 degrees.
Success guaranteed ! Here is the recipe in Dutch: Mini-kerstontbijtquiches - Recept - Allerhande - Albert Heijn[^]
|
|
|
|