Search code examples
c#entity-framework

Entity Framework same schema, different table, one sort function


Is it possible to have 2 different Entity Framework model for 2 different SQL Server Views, which have same schema, and have them go through a single sort function?

eg.

public class TableOne
{
    public string One { get; set; }
    public string Two { get; set; }
}

public class TableTwo
{
    public string One { get; set; }
    public string Two { get; set; }
}

public class DbContext
{
    public DbSet<TableOne> TableOne { get; set; }
    public DbSet<TableTwo> TableTwo { get; set; }
}

IEnumerable<TableOne/TableTwo> SortFunction(IEnumerable<TableOne/TableTwo> list)

Solution

  • Add interface:

    public interface ITable
    {
        string One { get; }
        string Two { get; }
    }
    
    public class TableOne : ITable {
        public string One {get; set;}
        public string Two {get; set;}
    }
    
    public class TableTwo : ITable {
        public string One {get; set;}
        public string Two {get; set;}
    }
    
    IEnumerable<ITable> SortFunction(IEnumerable<ITable> list)
    

    And maybe, you should use IQueryable<ITable>:

    IEnumerable<ITable> SortFunction(IQueryable<ITable> list)