I get this error:
ex = {"The Equal binary operator is not defined for the types" MySite.Domain.DomainModel.EntityFramework.NickName "and" System.Int32 "." }
What I tried to do was do everything where NickNameId = someIntPassedIn ... the problem is that NickNameId is a foreign key, so when it compares someIntPassedIn with NickNameId , it pulls out the whole NickName , which NickNameId refers NickNameId and tries to compare int with this object.
I need a solution here to allow it to compare int with id of NickName object ... so
A) How to define a binary Equal operator to compare these two objects
OR
B) How can I compare it directly with id instead of the whole object?
You don't have to read this, but here the SelectAllByKey method helps:
(I went to "NickNameId" and "1")
public IList<E> SelectAllByKey(string columnName, string key) { KeyProperty = columnName; int id; Expression rightExpr = null; if (int.TryParse(key, out id)) { rightExpr = Expression.Constant(id); } else { rightExpr = Expression.Constant(key); } // First we define the parameter that we are going to use the clause. var xParam = Expression.Parameter(typeof(E), typeof(E).Name); MemberExpression leftExpr = MemberExpression.Property(xParam, this._KeyProperty); int temp; BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr); //Create Lambda Expression for the selection Expression<Func<E, bool>> lambdaExpr = Expression.Lambda<Func<E, bool>>(binaryExpr, new ParameterExpression[] { xParam }); //Searching .... IList<E> resultCollection = ((IRepository<E, C>)this).SelectAll(new Specification<E>(lambdaExpr)); if (null != resultCollection && resultCollection.Count() > 0) { //return valid single result return resultCollection; }//end if return null; }
Let me know if you need more information.
Thanks,
Matt
source share