Comparing two objects to check whether they are different - c#-4.0

I have a method that accepts two parameters. Projecthealthnotes is my model.
I will like to compare objprojHealth with the getRow object that I am retrieving from the database.
If they are the same no need to call SaveChanges() and if not the same then call SaveChanges()
How can I compare these two objects and check whether they are the same?
public string WriteProgressHealthInfoToDb(Projecthealthnotes objprojHealth, string typeOfOperation)
{
using (var dbCntxt = new PPMSEntities1())
{
tbl_Project_Status_MSTR psmTable;
var convertedId = Convert.ToInt64(objprojHealth.Id);
var getRow = dbCntxt.tbl_Project_Status_MSTR.Single(m => m.ProjectStatusID == convertedId);
getRow.RecentProgress = objprojHealth.Recentprogress;
getRow.ObstaclesRisks = objprojHealth.Obstaclesrisk;
getRow.NextSteps = objprojHealth.Nextsteps;
getRow.ForWeekEnding = Convert.ToDateTime(objprojHealth.Weekendingdate);
getRow.BudgetHealth = Translator(objprojHealth.BudgetHealth);
getRow.TeamHealth = Translator(objprojHealth.TeamHealth);
getRow.RiskHealth = Translator(objprojHealth.RiskHealth);
getRow.ArtifactHealth = Translator(objprojHealth.BenefitHealth);
getRow.ScopeHealth = Translator(objprojHealth.ScopeHealth);
getRow.ScheduleHealth = Translator(objprojHealth.ScheduleHealth);
getRow.Phase = objprojHealth.Phase;
getRow.ReportingPeriod = Convert.ToInt16(objprojHealth.Reportingperiod);
//dbCntxt.Entry(getRow).State = System.Data.Entity.EntityState.Modified;
dbCntxt.SaveChanges();
return "success";
}

The only way to do this is to write a function that uses reflection to compare each property. Depending on your object you may have to do something a little more complex if it has nested reference types. The link below has some examples
http://www.sidesofmarch.com/index.php/archive/2007/08/03/use-reflection-to-compare-the-properties-of-two-objects/

Related

NODE.JS: iterating over an array of objects, creating a new key if it does not exist

I am iterating over a collection of data, in my case, an array of objects. Here is a sample of 2 data points from it:
{
violation_id: '211315',
inspection_id: '268804',
violation_category: 'Garbage and Refuse',
violation_date: '2012-03-22 0:00',
violation_date_closed: '',
violation_type: 'Refuse Accumulation' },
{
violation_id: '214351',
inspection_id: '273183',
violation_category: 'Building Conditions',
violation_date: '2012-05-01 0:00',
violation_date_closed: '2012-04-17 0:00',
violation_type: 'Mold or Mildew' }
I need to create a new array of objects from this, one for each "violation_category" property. If Violation category already exists in the new array I am creating, i simply add the information to that existing category object (instead of having two "building conditions" objects for example, I would just add to an existing one).
However, I am having trouble assigning to the existing object if the current one exists (it's easy to check if it does not, but not the other way around). This is what am attempting to do currently:
if (violationCategory.uniqueCategoryName) {
violationCategory.uniqueCategoryName.violations = results[i].violation_id;
violationCategory.uniqueCategoryName.date = results[i].violation_date;
violationCategory.uniqueCategoryName.closed =
results[i].violation_date_closed;
} else {
category.violations = results[i].violation_id;
category.date = results[i].violation_date;
category.closed = results[i].violation_date_closed;
violationCategory.push(category);
}
In first condition, if this category (key) exists, I simply add to it, and in the second condition, this is where I am struggling. Any help appreciated. Thanks guys.
Just add an empty object to the key if there no object there :
violationCategory.uniqueCategoryName = violationCategory.uniqueCategoryName || {};
And only then, add the data you want to the object.
violationCategory.uniqueCategoryName.violations = results[i].violation_id;
violationCategory.uniqueCategoryName.date = results[i].violation_date;
violationCategory.uniqueCategoryName.closed =
results[i].violation_date_closed;
No condition needed.
Good luck!
Assuming that you have an input variable which is an array of objects, where the objects are looking like the objects of the question, you can generate your output like this:
var output = {};
for (var item of input) {
if (!output[item.violation_category]) output[item.violation_category] = [];
output[item.violation_category].push(item);
}
Of course you might customize it like you want.

How to get object type

Given an arbitrary array of kineticjs objects there a way to get the object type?
var ary = new Array();
ary[0] = "Circle Obj";
ary[1] = "rect Obj";
ary[2] = "arc Obj";
ary[0] = "Circle Obj";
Please comment
Yes, use myObject.className
ary[0]=new Kinetic.Circle({...});
console.log(ary[0].className); // returns "Circle"

Sharepoint. External Content Types. Passing values to stored procedure

I have simple stored procedure, that takes couple parameters and updates table.
How to pass parameters via BDC?
For example, to execute stored procedure, that selects rows and takes one param, code below.
BdcService bdcservice = SPFarm.Local.Services.GetValue<BdcService>();
IMetadataCatalog catalog = bdcservice.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
// entity.GetLobSystem().GetLobSystemInstances()[0].Value;
IEntity entity = catalog.GetEntity(Utils.EntityNamespace, "GetMessage");
ILobSystemInstance lobSystemInstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;
IFilterCollection filters = entity.GetDefaultFinderFilters();
ComparisonFilter filter = (ComparisonFilter)filters[0];
filter.Value = code;
IEntityInstanceEnumerator enumerator = entity.FindFiltered(filters, lobSystemInstance);
DataTable result = entity.Catalog.Helper.CreateDataTable(enumerator);
DataTable result contains selected rows.
But how to pass couple parameters to Update procedure?
BdcService bdcservice = SPFarm.Local.Services.GetValue<BdcService>();
IMetadataCatalog catalog = bdcservice.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
// entity.GetLobSystem().GetLobSystemInstances()[0].Value;
IEntity entity = catalog.GetEntity(Utils.EntityNamespace, "ContractAdd");
ILobSystemInstance lobSystemInstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;
// entity.ExecuteScalar();
entity has method "ExecuteScalar", but how to pass params via this method?

Subsonic 3 - Sequence contains no matching element

I need help creating a LINQ SQL with subsonic. First the basics, this works fine:
var query = (from o in bd.concelhos
orderby o.descricao
select o);
var results = query.ToList<concelhos>();
However, I want to filter out some columns and I have created the following code:
var query = (from o in bd.concelhos
orderby o.descricao
select new FilteredConcelhos { id = o.idDistrito + "/" + o.idConcelho, descricao = o.descricao });
var results = query.ToList<FilteredConcelhos>();
which errors out in the ToList method with the description "Sequence contains no matching element"
Any help would be great with this...
update:
Turns out I was missing get set attributes in the newly declared class...
Like so
public class FilteredConcelhos
{
public string id { get; set; }
public string descricao { get; set; }
}
This clears the exception, but the resulting List is still all wrong (FilteredConcelhos.id contains nothing and FilteredConcelhos.descricao contains numbers)
Can you try to first execute the ToList and the select afterwards - then the select is performed via linq 2 objects!
Have you tried to work with an anonymous type?
var query = (from o in bd.concelhos
orderby o.descricao
select new { id = o.idDistrito + "/" + o.idConcelho,
descricao = o.descricao });
var results = query.ToList();
Unfortunately, this happened to me a lot. I'm not sure about the details of how Linq 2 Object works, but if you'll call ToList on the original object, like this:
from o in bd.concelhos.ToList()
...
It should do the trick.

Re-use of database object in sub-sonic

Yet another newbie SubSonic/ActiveRecord question. Suppose I want to insert a couple of records, currently I'm doing this:
using (var scope = new System.Transactions.TransactionScope())
{
// Insert company
company c = new company();
c.name = "ACME";
c.Save();
// Insert some options
company_option o = new company_option();
o.name = "ColorScheme";
o.value = "Red";
o.company_id = c.company_id;
o.Save();
o = new company_option();
o.name = "PreferredMode";
o.value = "Fast";
o.company_id = c.company_id;
o.Save();
scope.Complete();
}
Stepping through this code however, each of the company/company_option constructors go off and create a new myappDB object which just seems wasteful.
Is this the recommended approach or should I be trying to re-use a single DB object - and if so, what's the easiest way to do this?
I believe you can use the same object if you want to by setting its IsNew property to true, then change its data properties, save it again, repeat. Easy enough.
I 'm not so sure that you should bother, though. It depends on how bad those constructors are hurting you.
In my eyes assigning multiple objects to a single var is never a good idea but thats arguable. I would do this:
// Insert some options
company_option o1 = new company_option();
o1.name = "ColorScheme";
o1.value = "Red";
o1.company_id = c.company_id;
o1.Save();
company_option o2 = new company_option();
o2.name = "PreferredMode";
o2.value = "Fast";
o2.company_id = c.company_id;
o2.Save();
I you are worried about performance, that shouldn't be a issue unless you want to insert or update many objects at once. And again, in this case the time used for inserting the data takes longer than for the object creation.
If you are worried about performance you can skip the object creation and saving part completly by using a Insert query:
http://www.subsonicproject.com/docs/Linq_Inserts
db.Insert.Into<company_option>(
x => x.name,
x => x.value,
x => x.company_id)
.Values(
"ColorScheme",
"Red",
c.company_id
).Execute();

Resources