How to write my own comparison (definition for the Equal binary operator) for an entityframework for int?

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

0
source share
2 answers

You must call SelectAllByKey('NickName.ID','1') .

Since the ID is a property of the property, you can use this extension method:

 public static MemberExpression PropertyOfProperty(this Expression expr,string propertyName) { var properties = propertyName.Split('.'); MemberExpression expression = null; foreach (var property in properties) { if (expression == null) expression = Expression.Property(expr, property); else expression = Expression.Property(expression, property); } return expression; } 
+2
source

The accepted answer seems too complicated for the problem if I read it correctly.

If you understand correctly, you are trying to run a query, for example:

 var q = from e in Context.SomeEntities where e.NickNameId == someIntPassedIn select e; 

... but this will not work, because e.NickNameId is an entity, not an integer.

To refer to the Id property, you can simply refer to it, for example:

 var q = from e in Context.SomeEntities where e.NickNameId.Id == someIntPassedIn select e; 

Update: If you cannot use strongly typed properties because of your level of abstraction (for your comment), use query building methods :

 var q = (ObjectQuery<T>)Repository.SelectSomething(); return q.Where("it.NickName.Id = " + someIntPassedIn.ToString()); 

You can adapt this as you see fit, but the common point is that EF already knows how to translate strings into property elements.

+2
source

Source: https://habr.com/ru/post/1304819/


All Articles