我如何将用于objectframework对象的自定义比较(二进制运算符Equal的定义)写入一个int? [英] How can I write custom comparison (definition for binary operator Equal) for entityframework object to an int?

查看:97
本文介绍了我如何将用于objectframework对象的自定义比较(二进制运算符Equal的定义)写入一个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:


ex = {未为类型'MySite.Domain定义二进制运算符Equal .DomainModel.EntityFramework.NickName'和'System.Int32'。}

ex = {"The binary operator Equal is not defined for the types 'MySite.Domain.DomainModel.EntityFramework.NickName' and 'System.Int32'."}

我试图做的是在所有位置进行全选 NickNameId = someIntPassedIn ...的问题是,NickNameId是外键,因此当它比较 someIntPassedIn 时到 NickNameId 的情况下,它会拉取 NickNameId NickName 对象>引用并尝试将int与该对象进行比较。

What I tried to do was do a select all where the NickNameId = someIntPassedIn... the problem is that the NickNameId is a foreign key, so when it compares the someIntPassedIn to the NickNameId it pulls the whole NickName object that the NickNameId refers to and tries to compare the int to that object.

我需要一种解决方案,允许它将int与NickName对象的ID进行比较...所以

I need a solution here to allow it to compare the int to the NickName object's Id... so

A)如何定义二进制运算符等于比较这两个对象

A) How can I define the binary operator Equal for comparing these two objects

OR

B)我如何直接将其与id而不是整个对象进行比较?

B) How can I compare it directly to the id instead of the whole object?

您不必阅读本文,但这是SelectAllByKey方法od如果有帮助的话:
(我传入了 NickNameId和 1)

You don't have to read this, but here's the SelectAllByKey method incase it helps:
(I passed in "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 any more info.

谢谢,

马特

Thanks,
Matt

推荐答案

您应该致电 SelectAllByKey('NickName.ID','1')

由于 ID 是财产的财产,因此可以使用以下扩展方法:

Since ID is property of property, you could 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;
}

这篇关于我如何将用于objectframework对象的自定义比较(二进制运算符Equal的定义)写入一个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆