I found some questions that looked similar, but not exactly the same so I’ll go for it.
I’m using EF to retrieve 2 tables. These tables/entities have no real “is a” relationship. They just happen to have a couple of common fields which I want to expose in a list containing the first N entries of a combination between the two tables. Each row has to have some way of identifying which type it is and which instance it points to.
I have solved the problem, but I guess I was wondering if there was a better way. My solution was to create a ViewModel class:
intenal class EntityAEntityBCombination
{
public int? EntityAID { get; set; }
public int? EntityBID { get; set; }
public string CommonProperty { get; set; }
}
Then I did this:
var results = (
from a in EntityAList select new EntityAEntityBCombination
{ EntityAID = a.Id, EntityBID = null, CommonProperty = a.CommonProperty }
).Concat(
from b in EntityBList select new EntityAEntityBCombination
{ EntityAID = null, EntitiyBID = b.Id, CommonProperty = b.CommonProperty }
).Fetch(N)
It works, but seems dirty. Any suggestions?
Have a look at this, perhaps it doesn’t work straight out of the box, but it should give you an idea: