如何将实体框架对象的自定义比较(二元运算符 Equal 的定义)编写为 int?
我收到此错误:
ex = {“没有为类型 'MySite.Domain.DomainModel.EntityFramework.NickName' 和 'System.Int32' 定义二元运算符 Equal。”}
我尝试做的是选择所有 NickNameId = someIntPassedIn
...问题在于 NickNameId 是外键,因此当它将 someIntPassedIn
与 NickNameId
进行比较时,它会提取整个
对象并尝试将 int 与该对象进行比较。NickNameId
引用的 NickName
我在这里需要一个解决方案,以允许它将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该调用
SelectAllByKey('NickName.ID','1')
。由于
ID
是属性的属性,因此您可以使用以下扩展方法:You should call
SelectAllByKey('NickName.ID','1')
.Since
ID
is property of property, you could use this extension method:如果我正确地阅读了这篇文章,那么对于当前的问题来说,接受的答案似乎太复杂了。
如果我理解正确的话,您正在尝试运行如下查询:
...但这行不通,因为
e.NickNameId
是一个实体,而不是整数。要引用
Id
属性,您只需引用它即可,如下所示:更新: 如果由于您的抽象级别而无法使用强类型属性(根据您的评论),然后使用查询生成器方法:
您可以调整此正如您认为合适的那样,但总的一点是 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:
...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:Update: If you can't use strong-typed properties due to your level of abstraction (per your comment), then use query builder methods:
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.