I need to write a generic Join() function to perform a query between two DBSets, entities of type TEntity and parentEntities of type TParent. What this will do is get me an IQueryable of cObjectNames, each object with the PK of the entity and the name of the parent entity. both types have an IBaseEntity interface so the Id column is available but I need a way to generically specify the foreign key column in entities (fkCol in the example) and the parentEntities name column (parentNameCol).
public static IQueryable<cObjectNames> Join<TEntity, TParent>(IQueryable<TEntity> entities, IQueryable<TParent> parenEntities,
string fkCol, string parentNameCol)
where TEntity : class, IBaseEntity where TParent : class, IBaseEntity
{
IQueryable<cObjectNames> qNames = entities.Join(parenEntities, e => e.fkCol, p => p.Id, (e, p) =>
new cObjectNames() { name = p.parentNameCol, eId = e.Id });
return qNames;
}
I know it is possible to use EF to get the parent object, but I need a generic solution for several such fk relationships, where even the parent name column is not constant. And please save me the Dynamic LINQ suggestions - LINQ generic expressions are so much cooler...
The definition of cObjectNames is
public class cObjectNames
{
public int eId{ get; set; }
public string name{ get; set; }
}
and the IBaseEntity interface is:
public interface IBaseEntity
{
int Id { get; set; }
DateTimeOffset Created { get; set; }
DateTimeOffset? Lastupdated { get; set; }
DateTimeOffset? Deleted { get; set; }
}
Thanks!