如何将实体框架对象的自定义比较(二元运算符 Equal 的定义)编写为 int?

发布于 2024-08-15 02:08:02 字数 1931 浏览 13 评论 0原文

我收到此错误:

ex = {“没有为类型 'MySite.Domain.DomainModel.EntityFramework.NickName' 和 'System.Int32' 定义二元运算符 Equal。”}

我尝试做的是选择所有 NickNameId = someIntPassedIn...问题在于 NickNameId 是外键,因此当它将 someIntPassedInNickNameId 进行比较时,它会提取整个 NickNameId 引用的 NickName 对象并尝试将 int 与该对象进行比较。

我在这里需要一个解决方案,以允许它将 int 与 NickName 对象的 Id 进行比较...所以

A) 如何定义二元运算符 Equal 来比较这两个对象

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

您不必阅读本文,但这里有 SelectAllByKey 方法,以防它有帮助:
(我传入了“NickNameId”和“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;
    }

如果您需要更多信息,请告诉我。

谢谢,
马特

I'm getting this error:

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

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.

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

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

OR

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-08-22 02:08:02

您应该调用 SelectAllByKey('NickName.ID','1')

由于 ID 是属性的属性,因此您可以使用以下扩展方法:

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;
}

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

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;
}
原野 2024-08-22 02:08:02

如果我正确地阅读了这篇文章,那么对于当前的问题来说,接受的答案似乎太复杂了。

如果我理解正确的话,您正在尝试运行如下查询:

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

...但这行不通,因为 e.NickNameId 是一个实体,而不是整数。

要引用 Id 属性,您只需引用它即可,如下所示:

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

更新: 如果由于您的抽象级别而无法使用强类型属性(根据您的评论),然后使用查询生成器方法

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

您可以调整此正如您认为合适的那样,但总的一点是 EF 已经知道如何将字符串转换为属性成员。

The accepted answer seems way too complicated for the problem at hand, if I'm reading this correctly.

If I understand you correctly, you're trying to run a query like:

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

...but this won't work, because e.NickNameId is an entity, not an integer.

To reference the Id property, you can just refer to it, like this:

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

Update: If you can't use strong-typed properties due to your level of abstraction (per your comment), then use query builder 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 general point is that the EF already knows how to translate strings to property members.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文