Note that there are some explanatory texts on larger screens.

plurals
  1. POExpression generated based on interface
    text
    copied!<p>I'm geting the exception <strong>Unable to cast the type 'MySomeTypeThatImplementsISomeInterfaceAndIsPassedAs[T]ToTheClass' to type 'ISomeInterface'. LINQ to Entities only supports casting Entity Data Model primitive types.</strong></p> <p>my repository looks like</p> <pre><code>public interface IRepository&lt;T&gt; { IQueryable&lt;T&gt; Get(System.Linq.Expressions.Expression&lt;System.Func&lt;T, bool&gt;&gt; Query); } </code></pre> <p>Also, I have the service class</p> <pre><code>public abstract class FinanceServiceBase&lt;TAccount, TParcel, TPayment&gt; where TAccount : IAccount where TParcel : IParcel where TPayment : IPayment { //... IRepository&lt;TPayment&gt; ParcelRepository {get; private set;} public bool MakePayment(TPayment payment) { //... ParcelRepository.Get(p =&gt; p.ParcelId == 2); // here my exception is thrown // **p.ParcelId is in IParcel** //... } } //... </code></pre> <p>With this class I can control many things about finances without rewrite code for other programs. I've did the class with 3 generic parameters because I couldn't use IRepository because my repository can't be <code>&lt;out T&gt;</code></p> <p><strong>ParcelId is Int32</strong><br/> TParcel is typeof(<em>ParcelToReceive</em>) that is an entity who implement <em>IParcel</em>, and was generated with codeonly</p> <p>The problem occurs when I call Get and the resultant lambda looks like</p> <pre><code>(**(ISomeInterface)**$p).SomeInterfaceMember == </code></pre> <p>instead of</p> <pre><code>($p.SomeInterfaceMember) </code></pre> <p>so, entity framework try do the cast and throws the exception. What I want to know is: is there anyway to tell linq that the lambda field p.ParcelId is from <strong>TParcel</strong> and not from <strong>IParcel</strong>. </p> <p>Already tried (with no luck):</p> <pre><code>p =&gt; ((TParcel)p).ParcelId </code></pre> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload