Casting From Type to Interface in C#

adamsbeaver

New Member
I am building an ASP.NET application which will query one of three tables which capture maufacturing errors in a product. Of course each table is reflected in an EF Entity. Which table will be queried is not determined until the page is submitted and a flag is set indicating which errors to query.Each of the three entities have many properties in common. These commonalities have been defined in an Interface and implemented by the Entities (via partial class definitions). So those partial classes look like ... \[code\]public partial class ProductException : IExceptionEntity { }public partial class PartException : IExceptionEntity { }public partial class RebuildException: IExceptionEntity { }\[/code\]In a repository class I have defined a generic method that will allow me to query the Exception Entity that I specify. This is neccessary as the WHERE clause passed in as a parameter to the method, may contain fields that are not defined in the Interface and are specific to the individual Exception entity (product, part, and rebuild). So calling those looks like .. \[code\]IQueryable<ProductException> prodQuery = Repository.GetFilteredQuery<ProductException>(query, whereClause);IQueryable<PartException> partQuery = Repository.GetFilteredQuery<PartException>(query, whereClause);IQueryable<RebuildException> rebuildQuery = Repository.GetFilteredQuery<RebuildException>(query, whereClause);\[/code\]At no time would the web page define more that one query, and what it would do with the results of the query is fairly similiar regardless of the results of the query, so I would like to be able to assign a variable/property that could use to access the query/results regardless of whcih entity was being queried. Something like below ... \[code\] protected override void OnPreLoad(EventArgs e) { InitializeExceptionQuery(); } List<IExceptionEntity> _exceptionList = null; public List<IExceptionEntity> ExceptionList { get { if(_exceptionList == null) _exceptionList = LookupQuery.ToList(); return _exceptionList; } } IQueryable<IExceptionEntity> _query = null; public IQueryable<IExceptionEntity> LookupQuery { get { if(_query == null) _query = GetExceptionQuery(this.ExceptionCategory); return _exceptionList; } set { this._query = value; } } private void InitializeExceptionQuery() { String whereClause = "Filters defined here from user control properties."; switch (this.ExceptionCategory) { case ExceptionCategories.Brewer: this.LookupQuery = base.Repository.GetFilteredQuery<ProductException>(this.LookupQuery, whereClause, this.SortField, this.SortDirection).Cast<IExceptionEntity>(); break; case ExceptionCategories.Part: this.LookupQuery = base.Repository.GetFilteredQuery<PartException>(this.LookupQuery, whereClause, this.SortField, this.SortDirection).Cast<IExceptionEntity>(); break; case ExceptionCategories.Rework: this.LookupQuery = base.Repository.GetFilteredQuery<RebuildException>(this.LookupQuery, whereClause, this.SortField, this.SortDirection).Cast<IExceptionEntity>(); break; case ExceptionCategories.UNKNOWN: throw new ArgumentException("Exception Category is undefined"); } } private void BindQueryResults() { switch (this.ExceptionCategory) { case ExceptionCategories.Brewer: listView.DataSource = this.ExceptionList.Cast<ProductException>(); listView.DataBind(); break; case ExceptionCategories.Part: listView.DataSource = this.ExceptionList.Cast<PartException>(); listView.DataBind(); break; case ExceptionCategories.Rework: listView.DataSource = this.ExceptionList.Cast<RebuildException>(); listView.DataBind(); break; case ExceptionCategories.UNKNOWN: throw new ArgumentException("Exception Category is undefined"); } }\[/code\]The whole goal is that I want to be able to call the ExceptionList property, cast it to one of the types that implement the IExceptionEntity interface and Voila! no matter which of the three entities I am querying there is a single point from which I can retreive results ... i.e. see BindQueryResults().This, however is not the case. When I run this, the ExceptionList property is throwing an exception when it attempts to convert the Query to a list (.ToList())\[code\]Unable to cast the type 'PartException' to type 'IExceptionEntity'. LINQ to Entities only supports casting EDM primitive or enumeration types. \[/code\]I thought that you could cast an Interface and the classes that implement it back and forth as neccessary. Clearly this is not the case and I suppose that it makes sense, but is there any other way to accomplish what I am attempting to do? I gotta believe that there is. Does anybody else have this same conundrum?Thank you,G
 
Back
Top