Axapta operator overloading - operator-overloading

I was wondering if it is possible to overload the == operator in x++.
In C# I would do it like this
public static bool operator == (SomeObject obj1, SomeObject obj2)
{
bool status = false;
//Compare the objects
return status;
}

Operator overloading is not supported.
See this list for other differences to C#.

Related

What's the point of using the bitwise XOR operator "^" to get/generate the hashcode of an object?

While reading some code, it came to my attention that some developers use the bitwise XOR operator, ^, to generate the hashcode of an object.
What's the point of doing it like this? Does it have some advantages over other methods to get/generate the hashcode of an object?
Here is a code example.
class Student {
final String name;
final int age;
Student(this.name, this.age);
#override
bool operator ==(other) {
return (other is Student) && other.name == name && other.age == age;
}
#override
int get hashCode => age.hashCode ^ name.hashCode; // <-- Here
}
It has to be a single number, and the more it varies on more of the bits of any member of the object, the better.

How to construct Where Expression dynamically in Entity Framework?

I've taken a look at this answer on how to dynamically create an OrderBy expression in Entity Framework. But I'd like to also build a dynamic Where expression. Something along the lines of this:
public IEnumerable<InventoryItem> GetAll(string filterBy, object value)
{
var results = new List<InventoryItem>();
using (var db = new InventoryDb())
{
if (QueryHelper.PropertyExists<InventoryItem>(filterBy))
{
var query = db.rri_InventoryItems.WhereByProperty(filterBy, value);
foreach(var item in query.Where(expr))
{
results.Add(ConvertItem(item));
}
}
}
return results;
}
Passing in the property to filter by and a value as ab object. Queryable has two methods for Where that both take two parameters, so I am not even sure which is the proper one.
And it's at this point I get a little more lost. I'm not sure how to refactor the original OrderByProerty method to provide a WhereByProperty. I know what I have here is completely wrong. I'm not sure what to do with it.
Ideally, I'd want to extend this even more by providing a collection of objects that could be used to build a query with ands and or operators.
Queryable has two methods for Where that both take two parameters, so I am not even sure which is the proper one.
You need the one that receives Expression<Func<T, bool>> predicate.
Here is how you can build dynamically a predicate similar to (T item) => item.Property == value:
public static partial class QueryableExtensions
{
public static IQueryable<T> WhereEquals<T>(this IQueryable<T> source, string member, object value)
{
var item = Expression.Parameter(typeof(T), "item");
var memberValue = member.Split('.').Aggregate((Expression)item, Expression.PropertyOrField);
var memberType = memberValue.Type;
if (value != null && value.GetType() != memberType)
value = Convert.ChangeType(value, memberType);
var condition = Expression.Equal(memberValue, Expression.Constant(value, memberType));
var predicate = Expression.Lambda<Func<T, bool>>(condition, item);
return source.Where(predicate);
}
}
I've tried to write it in such a way so you can step over the code in order to understand what it does. The only line that might need some explanation is:
var memberValue = member.Split('.').Aggregate((Expression)item, Expression.PropertyOrField);
This is a simple way of handling nested properties like obj.Prop1.Prop2 etc. If you don't need such capability, you can simply use this instead:
var memberValue = Expression.PropertyOrField(item, member);
I didn't need nested properties (yet). I modified your code slightly and have this that is working:
public static IQueryable<T> WhereEquals<T>(
this IQueryable<T> source, string propertyName, object value)
{
if (typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase |
BindingFlags.Public | BindingFlags.Instance) == null)
{
return null;
}
ParameterExpression parameter = Expression.Parameter(typeof(T), "item");
Expression whereProperty = Expression.Property(parameter, propertyName);
Expression constant = Expression.Constant(value);
Expression condition = Expression.Equal(whereProperty, constant);
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(condition,parameter);
return source.Where(lambda);
}

Drools Condition matching failing

I am using Drools 4 and when I am comparing two hashmap values with == it is not working. All other operators like >=,<=,<,>,!= are working fine. I am comparing as below. I am using map as . Is there any thing wrong with the statement. The map values will be updated in actions of the rules. I am able to see the updated values in map but comparison of the values is failing. I am using update(abc) to update the values.
eval((abc.getValue("123")).intValue() == (abc.getValue("456")).intValue())
Rule:
rule "008"
salience -18
agenda-group "CAP"
auto-focus true
when
testObj: TestObj(eval(fireNextPriority==true), categoryCount==18,
eval(!firedRules.contains(Integer.valueOf(23449)))
&& (date >= 1263925800000)
&& (date <= 4102338600000) && (date >= 1263925800000) && (date <= 4102338600000)
&& eval(1 == 1)
&& eval(testObj.getVariableValue("C1TC") == testObj.getVariableValue("Y1TC")))
then
System.out.println("Firing rule: CAP - 008");
testObj.setStatus(true);
testObj.setPriority(1);
testObj.addToFiredRules(23449);
update(testObj);
testObj.addVariableValue("C1PC", testObj.getVariableValue("C1PC")-
testObj.getVariableValue("C1OF"));
end
Object which we are using:
public class TestObj{
Long date;
Integer categoryCount;
boolean status = false;
boolean executeFinalRule = false;
boolean executeFinalRuleForCatg = true;
boolean fireNextPriority = true;
Set<Integer> firedRules = new HashSet<Integer>();
private int priority;
Map<String, Integer> variableValues = new HashMap<String, Integer>();
public Integer getCategoryCount() {
return categoryCount;
}
public void setCategoryCount(Integer categoryCount) {
this.categoryCount = categoryCount;
}
public void increaseCategoryCount(){
this.categoryCount++;
}
public void addVariableValue(String variableCode, Integer count){
if (count < 0) count = 0;
this.variableValues.put(variableCode, count);
}
public Integer getVariableValue(String variableCode){
Integer value = this.variableValues.get(variableCode);
return value == null? 0 : value;
}
public boolean isStatus() {
return status;
}
public Set<Integer> getFiredRules() {
return firedRules;
}
public void setFiredRules(Set<Integer> firedRules) {
this.firedRules = firedRules;
}
public void addToFiredRules(int l){
this.firedRules.add(l);
}
}
Thanks in advance.
I've never used Drools 4.x, so these are observations based of my (old) knowledge of 5.1.1 - they may not be accurate in your case.
eval(fireNextPriority==true)
It's remarkable that this works. eval does not maintain the context of the enclosing pattern (fact), so normally you'd have to write
eval(!firedRules.contains(Integer.valueOf(23449))
Same thing.
eval(testObj.getVariableValue("C1TC") == testObj.getVariableValue("Y1TC"))
This is as I'd write it from 5.1 onwards.
The strange thing is
testObj.addVariableValue("C1PC", testObj.getVariableValue("C1PC")
testObj.getVariableValue("C1OF"));
(DID I MESS THIS UP WHILE EDITING?) These changes are after the update call, which means that they won't be seen by the engine. Has the update of map entries "C1TC" and/or "Y1TC" been done likewise in another rule? Then they won't be equal to Drools, even if they are to Java...

Linq Expression building for Entity Framework involving complex object

I am using Entity Framework version 4. I need to compare a large (~1 million record) SQL Server table to a longish (~2000) array of complex objects returned from a web service. Five different properties need to be compared to determine whether an instance of the complex object is already in the database.
I created a function that returns an expression for use in .Where and .Any methods. It looks like this (where A is the complex object, and tblA is the EF class):
function Expression<tblA, bool> GetSearchPredicate(A a)
{
return ta => ta.Field1.Equals(a.Field1)
&& ta.Field2.Equals(a.Field2)
&& ta.Field3.Equals(a.Field3)
&& ta.Field4.Equals(a.Field4)
&& ta.Field5.Equals(a.Field5);
}
This works. And I can compare all 2000 instances of A by doing this:
IEnumerable<A> objects = [web service call];
var result = objects.Select(a => !db.tblA.Any(GetSearchPredicate(a)));
That works, too. But it's slow. So I looked into building a utility method that could build an expression that could be transmitted down to the database directly through EF.
I used the code in this question as a basis for building that utility method. The example in that question shows comparing a single property to a series of constants, while my version would have to compare multiple properties to multiple constants. My best effort is below:
public static IQueryable<TEntity> WhereIn<TEntity>
(
this ObjectQuery<TEntity> query,
IEnumerable<Expression<Func<TEntity, bool>>> predicates
)
{
if (predicates == null) throw new ArgumentNullException("predicates");
IEnumerable<ParameterExpression> p = predicates.Select(pred => pred.Parameters.Single()).ToArray();
IEnumerable<Expression> equals = predicates.Select(value =>
(Expression)value.Body);
Expression bigEqual = equals.Aggregate((accumulate, equal) =>
Expression.Or(accumulate, equal));
var result1 = Expression.Lambda<Func<TEntity, bool>>(bigEqual, p.First());
var result = query.Where(result1);
return result;
}
This would be invoked like this:
IEnumerable<A> objects = [web service call];
var result = db.tblA.WhereIn(objects.Select(a => GetSearchPredicate(a)));
What I get is a message saying that "ta" (the placeholder for the TEntity object) is not bound. I thought this was because I had multiple expressions (the variable predicates) being combined, and maybe this message was being thrown because I was only passing the parameter from the first of the predicates IEnumerable. But this happens even if predicates is one expression long.
I am reasonably sure, based on the method I linked to, that I could build an expression comparing each of the five properties to a constant (the values of A.Field1 through A.Field5), rather than passing in the parameter predicates that already has them assembled into a series of expressions. But I would rather not, since that would require my method to know that it's working with types A and tblA, and that's the opposite of generic and general-purpose. (It'd also be complex and messy.)
I hope the examples I've shown explain what I want to do. Can it be done in a generic way?
You will need to replace the parameter in the predicate bodies with a single parameter. Something like this should work:
public static Expression<Func<T, bool>> BuildOr<T>(
IEnumerable<Expression<Func<T, bool>>> predicates)
{
Expression body = null;
ParameterExpression p = null;
Expression<Func<T, bool>> first = null;
foreach (Expression<Func<T, bool>> item in predicates)
{
if (first == null)
{
first = item;
}
else
{
if (body == null)
{
body = first.Body;
p = first.Parameters[0];
}
var toReplace = item.Parameters[0];
var itemBody = ReplacementVisitor.Transform(item, toReplace, p);
body = Expression.OrElse(body, itemBody);
}
}
if (first == null)
{
throw new ArgumentException("Sequence contains no elements.", "predicates");
}
return (body == null) ? first : Expression.Lambda<Func<T, bool>>(body, p);
}
private sealed class ReplacementVisitor : ExpressionVisitor
{
private IList<ParameterExpression> SourceParameters { get; set; }
private Expression ToFind { get; set; }
private Expression ReplaceWith { get; set; }
public static Expression Transform(
LambdaExpression source,
Expression toFind,
Expression replaceWith)
{
var visitor = new ReplacementVisitor
{
SourceParameters = source.Parameters,
ToFind = toFind,
ReplaceWith = replaceWith,
};
return visitor.Visit(source.Body);
}
private Expression ReplaceNode(Expression node)
{
return (node == ToFind) ? ReplaceWith : node;
}
protected override Expression VisitConstant(ConstantExpression node)
{
return ReplaceNode(node);
}
protected override Expression VisitBinary(BinaryExpression node)
{
var result = ReplaceNode(node);
if (result == node) result = base.VisitBinary(node);
return result;
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (SourceParameters.Contains(node)) return ReplaceNode(node);
return SourceParameters.FirstOrDefault(p => p.Name == node.Name) ?? node;
}
}
Your WhereIn method then becomes:
public static IQueryable<TEntity> WhereIn<TEntity>(
this ObjectQuery<TEntity> query,
IEnumerable<Expression<Func<TEntity, bool>>> predicates)
{
if (predicates == null) throw new ArgumentNullException("predicates");
var predicate = BuildOr(predicates);
return query.Where(predicate);
}

What is the best way to compare two entity framework entities?

I want to know the most efficient way of comparing two entities of the same type.
One entity is created from an xml file by hand ( ie new instance and manually set properties) and the other is retvied from my object context.
I want to know if the property values are the same in each instance.
My first thoughts are to generate a hash of the property values from each object and compare the hashes, but there might be another way, or a built in way?
Any suggestions would be welcome.
Many thanks,
James
UPDATE
I came up with this:
static class ObjectComparator<T>
{
static bool CompareProperties(T newObject, T oldObject)
{
if (newObject.GetType().GetProperties().Length != oldObject.GetType().GetProperties().Length)
{
return false;
}
else
{
var oldProperties = oldObject.GetType().GetProperties();
foreach (PropertyInfo newProperty in newObject.GetType().GetProperties())
{
try
{
PropertyInfo oldProperty = oldProperties.Single<PropertyInfo>(pi => pi.Name == newProperty.Name);
if (newProperty.GetValue(newObject, null) != oldProperty.GetValue(oldObject, null))
{
return false;
}
}
catch
{
return false;
}
}
return true;
}
}
}
I haven't tested it yet, it is more of a food for thought to generate some more ideas from the group.
One thing that might be a problem is comparing properties that have entity values themselves, if the default comparator compares on object reference then it will never be true. A possible fix is to overload the equality operator on my entities so that it compares on entity ID.
Override the Equals method of your object and write an implementation that compares the properties that make it equal.
public override bool Equals(object obj)
{
return MyProperty == ((MyObject)obj).MyProperty
}