I am trying to refactor some Entity Framework code where I have a Products entity and related entities for the Product's series, family, brand, etc. The series, family, and brand entities all have the same basic structure with an Id and Text property.
The two methods below are very similar and should be able to be refactored to a single method, but I'm not sure how to dynamically pass in which entity I'm querying from. In this case I need to be able to pass in db.ProductFamilies, or db.ProductSeriesSet. Any ideas how to make these 2 become 1?
Thanks!
public DTOs.ProductCollection GetFamily(int id)
{
using (Entities db = new Entities())
{
var fam = (from collection in db.ProductFamilies.Include("Product")
.Include("Product.Stuff1")
.Include("Product.Stuff2")
where collection.Id == id
select collection).FirstOrDefault();
return ProductCollectionEFToProductCollectionDTO(fam, true);
}
}
public DTOs.ProductCollection GetSeries(int id)
{
using (Entities db = new Entities())
{
var ser = (from collection in db.ProductSeriesSet.Include("Product")
.Include("Product.Stuff1")
.Include("Product.Stuff2")
where collection.Id == id
select collection).FirstOrDefault();
return ProductCollectionEFToProductCollectionDTO(ser, true);
}
}