how to pass viewmodel in to view - c#

I have a viewmodel called "PlanObjectsViewModel". I need to add results inside of my while loop to an instance of PlanObjectsViewModel.I tried following approach..Is that wrong?? I'm getting an exception called;
The model item passed into the dictionary is of type 'MvcApp.ViewModel.PlanObjectsViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApp.ViewModel.PlanObjectsViewModel]'.
var model2 = model1.GroupBy(t => t.Type).Select(g => new PlanBaseTypedObjects
{
Id = g.Key,
ObjectDetails = g
});
int Type1 = 0;
int Type2 = 0;
double? temp = 0;
foreach (var item in model2)
{
if(item.Id==1)
Type1 = item.ObjectDetails.Count();
if (item.Id == 2)
Type2 = item.ObjectDetails.Count();
}
Random rand = new Random();
var final = new PlanObjectsViewModel(); // want to add data to this view model
while (budget > temp)
{
if (Type1 != 0) {
int randi = rand.Next(0, Type1);
foreach (var item in model2)
{
if (item.Id == 1) { foreach (var x in item.ObjectDetails) {
if (x.Id == randi)
final.Id = x.Id;// i don't know,whether this is a correct way to
//add data row to the model
temp=temp+x.Price;
}
}
}
}
if (Type2 != 0)
{
int randi = rand.Next(0, Type2);
foreach (var item in model2)
{
if (item.Id == 2)
{
foreach (var x in item.ObjectDetails)
{
if (x.Id == randi)
final.Id = x.Id;
temp = temp + x.Price;
}
}
}
}
}
return View(model1);
In my view i have;
#model IEnumerable<MvcApp.ViewModel.PlanObjectsViewModel>
can anyone explain why is that?? if i'm doing it it a wrong way,what is the best approach??Thank you.

You're expecting a collection but you pass in an individual item to the view instead.
MvcApp.ViewModel.PlanObjectsViewModel = object
System.Collections.Generic.IEnumerable[MvcApp.ViewModel.PlanObjectsViewModel] = collection of objects
Change this
#model IEnumerable<MvcApp.ViewModel.PlanObjectsViewModel>
to
#model MvcApp.ViewModel.PlanObjectsViewModel

Related

C#, Newtonsoft, need to get array items not already handled

I have a json array that looks like...
{
"equipment": [{
"date_of_examination": "2022-05-20T14:08:38.072965",
"defect_type": ["DN"],
"eqpt_ref": "AA1",
"eqpt_name": ["2 Leg Chain Sling"],
"eqpt_manufacturer": "Merc",
"eqpt_model": "edes",
"part_no": "A1",
"serial_no": "A1",
"year": "2019",
"swl": "32 tons",
"exam_type": ["6 Months"],
"date_of_last_examination": "2021-11-20T00:00:00",
"date_of_next_examination": "2022-11-20T00:00:00",
"defect": "sling is torn",
"action_required": "replace"
}, {
"date_of_examination": "2022-05-20T14:12:23.997004",
"eqpt_ref": "AA2",
"eqpt_name": ["Other - "],
"eqpt_name_other": "widget",
"eqpt_manufacturer": "merc",
"eqpt_model": "edes",
"part_no": "B1",
"serial_no": "B1",
"year": "2019",
"swl": "32 tons",
"exam_type": ["6 Months"]
}, {
"date_of_examination": "2022-05-20T14:13:24.795136",
"defect_type": ["DF"],
"eqpt_ref": "CC1",
"eqpt_name": ["Endless Round Sling (2.5m)"],
"eqpt_manufacturer": "merc",
"eqpt_model": "edes",
"part_no": "c1",
"serial_no": "c1",
"year": "2019",
"swl": "42 tons",
"exam_type": ["6 Months"],
"defect": "stitching is coming undone",
"danger_value": "6",
"danger_units": ["Weeks"],
"action_required": "needs to be stitched again"
}]
}
I am attempting to loop through the array and filter items as I need, to populate a table later.
The table has three parts.
First, is show all items with a defect_type of "DN". Second is to show all defect_type of "DF", and the last part is to show all the rest (in his case, the one with eqpt_name of AA2)
My original code is...
for (int j = 0; j <= 2; j++)
{
// Note, some table name parts won't have the "Filter..." aspect
// the string below will change depending on which loop we are in.
string[] tableNameParts = "TableStart:equipment:defectNow:Filter:defect_type=DN".Split(':');
string tableNameJson = tableNameParts[1].Replace("»", "");
var jsonRows = IncomingJson[tableNameJson];
if (tableNameParts.Count() > 3)
{
// We probably have a filter set.
if (tableNameParts[3].Replace("»", "").ToLower() == "filter" && tableNameParts.Count() > 4)
{
// These values are not set in stone. It is what values have been set in the JSON, and then matched.
// for example... TableStart:<subform name>:<differentiator>:Filter:<field name>=<field value>
string[] FilterParts = tableNameParts[4].Split('=');
// Get the filter field and value to filter by
if (FilterParts.Count() > 1)
{
string FilterField = FilterParts[0].Replace("»", "");
string FilterValue = FilterParts[1].Replace("»", "");
JArray filteredArray = new JArray();
if (jsonRows[0].GetType() == typeof(JObject))
{
//int loopCount = 0;
foreach (JObject arrayObject in jsonRows) // Each group can have a set of arrays. (each record has multiple sub records)
//for (int i = 0; i < jsonRows.Count(); i++)
{
//JObject arrayObject = jsonRows[i];
foreach (var objectItem in arrayObject)
{
string objectItemValue = string.Empty;
if (objectItem.Value.GetType() == typeof(JArray))
{
foreach (var item in objectItem.Value)
{
objectItemValue += item;
}
}
else
{
objectItemValue = (string)objectItem.Value;
}
if (objectItem.Key == FilterField && objectItemValue == FilterValue)
{
// We need to save the item.
filteredArray.Add(arrayObject);
testArray.Add(arrayObject);
//arrayObject["filtered"] = true;
//IncomingJson[tableNameJson][loopCount]["filtered"] = true;
}
}
//loopCount++;
}
}
else
{
foreach (JArray arrayGroup in jsonRows) // The array group (e.g. fault_record_subform)
{
// We are looking through the json array, to find any rows that match our filter key and filter value.
// We will then add that into our jsonRows
//int loopCount = 0;
foreach (JObject arrayObject in arrayGroup) // Each group can have a set of arrays. (each record has multiple sub records)
{
foreach (var objectItem in arrayObject)
{
string objectItemValue = string.Empty;
if (objectItem.Value.GetType() == typeof(JArray))
{
foreach (var item in objectItem.Value)
{
objectItemValue += item;
}
}
else
{
objectItemValue = (string)objectItem.Value;
}
if (objectItem.Key == FilterField && objectItemValue == FilterValue)
{
// We need to save the item.
filteredArray.Add(arrayObject);
testArray.Add(arrayObject);
//arrayObject["filtered"] = true;
//IncomingJson[tableNameJson][loopCount]["filtered"] = true;
}
}
}
//loopCount++;
}
}
//filteredArray.CopyTo(testArray, 0);
jsonRows = filteredArray; // limit the jsonRows to the filtered set (overwrite the jsonRows)
}
}
}
else
{
// This is not a filter set
JArray singleArray = new JArray();
foreach(var arraySet in jsonRows)
{
if (!testArray.Intersect(arraySet).Any())
{
if (arraySet.GetType() == typeof(JObject))
{
singleArray.Add(arraySet);
}
else
{
foreach (JObject arrayObject in arraySet)
{
singleArray.Add(arrayObject);
}
}
}
}
jsonRows = singleArray;
}
}
By the time it gets to the "this is not a filter set" (which should be the third iteration of the loop), I need to be able to ignore the other filtered items, but as you might see, I have attempted to mark an item as filtered (then filter out). I have also tried to add the filtered items to an alternative array and use that to filter out. All to no avail.
How do I make it so that the "this is not a filter set" rows can ignore the rows already filtered?
=========== EDIT ==============
After reviewing the link from dbc to the fiddler (I don't have an account on there, and don't know how to link to my changes), I have it running in the fiddler with the code below.
JObject json = JObject.Parse(GetJson());
string[] tableNames = {"TableStart:equipment:defectNow:Filter:defect_type=DN","TableStart:equipment:defectFuture:Filter:defect_type=DF","TableStart:equipment:defectNone"};
for (int j = 0; j <= 2; j++)
{
// Note, some table name parts won't have the "Filter..." aspect
// the string below will change depending on which loop we are in.
string[] tableNameParts = tableNames[j].Split(':');
string tableNameJson = tableNameParts[1].Replace("»", "");
var jsonRows = json[tableNameJson];
if (tableNameParts.Count() > 3)
{
// We probably have a filter set.
if (tableNameParts[3].Replace("»", "").ToLower() == "filter" && tableNameParts.Count() > 4)
{
// These values are not set in stone. It is what values have been set in the JSON, and then matched.
// for example... TableStart:<subform name>:<differentiator>:Filter:<field name>=<field value>
string[] FilterParts = tableNameParts[4].Split('=');
// Get the filter field and value to filter by
if (FilterParts.Count() > 1)
{
string FilterField = FilterParts[0].Replace("»", "");
string FilterValue = FilterParts[1].Replace("»", "");
JArray filteredArray = new JArray();
if (jsonRows[0].GetType() == typeof(JObject))
{
//int loopCount = 0;
foreach (JObject arrayObject in jsonRows) // Each group can have a set of arrays. (each record has multiple sub records)
//for (int i = 0; i < jsonRows.Count(); i++)
{
//JObject arrayObject = jsonRows[i];
foreach (var objectItem in arrayObject)
{
string objectItemValue = string.Empty;
if (objectItem.Value.GetType() == typeof(JArray))
{
foreach (var item in objectItem.Value)
{
objectItemValue += item;
}
}
else
{
objectItemValue = (string)objectItem.Value;
}
if (objectItem.Key == FilterField && objectItemValue == FilterValue)
{
// We need to save the item.
filteredArray.Add(arrayObject);
//testArray.Add(arrayObject);
//arrayObject["filtered"] = true;
//IncomingJson[tableNameJson][loopCount]["filtered"] = true;
}
}
//loopCount++;
}
}
else
{
foreach (JArray arrayGroup in jsonRows) // The array group (e.g. fault_record_subform)
{
// We are looking through the json array, to find any rows that match our filter key and filter value.
// We will then add that into our jsonRows
//int loopCount = 0;
foreach (JObject arrayObject in arrayGroup) // Each group can have a set of arrays. (each record has multiple sub records)
{
foreach (var objectItem in arrayObject)
{
string objectItemValue = string.Empty;
if (objectItem.Value.GetType() == typeof(JArray))
{
foreach (var item in objectItem.Value)
{
objectItemValue += item;
}
}
else
{
objectItemValue = (string)objectItem.Value;
}
if (objectItem.Key == FilterField && objectItemValue == FilterValue)
{
// We need to save the item.
filteredArray.Add(arrayObject);
//testArray.Add(arrayObject);
//arrayObject["filtered"] = true;
//IncomingJson[tableNameJson][loopCount]["filtered"] = true;
}
}
}
//loopCount++;
}
}
//filteredArray.CopyTo(testArray, 0);
jsonRows = filteredArray; // limit the jsonRows to the filtered set (overwrite the jsonRows)
}
}
}
else
{
// This is not a filter set
JArray singleArray = new JArray();
foreach(var arraySet in jsonRows)
{
//if (!testArray.Intersect(arraySet).Any())
{
if (arraySet.GetType() == typeof(JObject))
{
singleArray.Add(arraySet);
}
else
{
foreach (JObject arrayObject in arraySet)
{
singleArray.Add(arrayObject);
}
}
}
}
jsonRows = singleArray;
}
}
What I need ultimately (the jsonRows will be used elsewhere in my code within the loop) is that the third set will have items not found in the first 2 sets.
After a bit of further experimentation, using dotnetfiddle as introduced to me by #dbc (thank you), I have created a List and added each arrayObject into the list during the filtering stages.
I then during the unfiltered stage check if my arraySet is contained in the List, and if not, then add that item to the remaining jsonRows, thereby giving me the balance of the original list.
As can be seen here...
https://dotnetfiddle.net/ot35Z2

Performing operations on a List<> created from ICollection objects - ICollection objects are being changed

I'm trying to create a string with parts and quantities made from data contained in an ICollection. I'm using a List to build the totals I need but when I perform operations on this List it's actually changing the values in the ICollection. I don't want those values changed. Code follows. PartsUsed is the ICollection. Is this because adding individual members of the collection to the list is only pointing to the original data?
private string PartsDetails(out int totalCount, String modtype)
{
totalCount = 0;
var str = new StringBuilder();
var usedParts = new List<PartUsed>();
var indexnum = 0;
foreach (var u in Rma.AssociatedUnits)
{
if (u.PartsUsed != null && u.PartsUsed.Count > 0)
{
if ((modtype == "ALL"))
{
foreach (var rep in u.PartsUsed)
{
if (!usedParts.Exists(x => x.Repair.Name == rep.Repair.Name))
{
usedParts.Add(rep);
}
else
{
usedParts[usedParts.FindIndex(f => f.Repair.Name == rep.Repair.Name)].RepairPartQuantity += rep.RepairPartQuantity;
}
}
}
}
}
foreach (var partsGroup in usedParts)
{
str.AppendFormat(str.Length > 0 ? Environment.NewLine + "{0} - {1}" : "{0} - {1}", partsGroup.RepairPartQuantity, partsGroup.Repair.Name);
totalCount += partsGroup.RepairPartQuantity;
}
return str.ToString();
}
It seems that your PartUsed is a class(e.g. reference type), so u.PartsUsed is actually a collection of references to some objects, so usedParts.Add(rep) is actually adding the same reference(object) to usedParts and when you get and modify one of them in usedParts[usedParts.FindIndex(f => f.Repair.Name == rep.Repair.Name)].RepairPartQuantity += rep.RepairPartQuantity you are actually modifying shared instance. This behavior can be demonstrated like this also:
class MyClass { public int Prop { get; set; } }
var ref1 = new MyClass{Prop = 1};
var ref2 = ref1;
ref2.Prop = 2;
Console.WriteLine(ref2.Prop);// prints 2
One way around would be to create a clone of PartUsed to put into usedParts but in your particular case it seems that you can use Dictionary<string, int>(assuming RepairPartQuantity is int) for usedParts. Something like this:
var usedParts = new Dictionary<string, int>();
.....
foreach (var rep in u.PartsUsed)
{
if (!usedParts.ContainsKey(rep.Repair.Name))
{
usedParts[rep.Repair.Name] = rep.RepairPartQuantity;
}
else
{
usedParts[rep.Repair.Name] += rep.RepairPartQuantity;
}
}
foreach (var kvp in usedParts)
{
str.AppendFormat(str.Length > 0 ? Environment.NewLine + "{0} - {1}" : "{0} - {1}", kvp.Value, kvp.Key);
totalCount += kvp.Value;
}

List<T>.addrange() not working

I have property :
public List<RequestCheckListDetail> DocumentChecklistMasterList
{
get
{
if (ViewState["DocumentChecklistMasterList"].IsObjectUsable())
_documentChecklistMasterList = (List<RequestCheckListDetail>)ViewState["DocumentChecklistMasterList"];
else
this._documentChecklistMasterList = new List<RequestCheckListDetail>();
return this._documentChecklistMasterList;
}
set { ViewState["DocumentChecklistMasterList"] = value; }
}
I am trying to add data to it using another list. However another list has different entity, so i am running loop over first list like:
List<RequestCheckListDetail> newList = new List<RequestCheckListDetail>();
int i = 0;
foreach (DocumentCheckListMaster item in list)
{
newList.Add(new RequestCheckListDetail
{
Id = i,
CheckListMaster = item
});
i++;
}
this.DocumentChecklistMasterList.AddRange(newList);
even if newList has items in it, DocumentChecklistMasterList always have 0 items.
I have tried following things:
List<RequestCheckListDetail> newList = new List<RequestCheckListDetail>();
int i = 0;
foreach (DocumentCheckListMaster item in list)
{
this.DocumentChecklistMasterList.Add(new RequestCheckListDetail
{
Id = i,
CheckListMaster = item
});
i++;
}
List<RequestCheckListDetail> newList = new List<RequestCheckListDetail>();
int i = 0;
foreach (DocumentCheckListMaster item in list)
{
this.DocumentChecklistMasterList.Insert(i,
new RequestCheckListDetail {
Id = i,
CheckListMaster = item
});
i++;
}
None of these codes are working properly.
I am still not able to add items to DocumentChecklistMasterList
Please help me:
EDIT:
IsObjectUsable() is extension method i have added to check if object is null
public static bool IsObjectUsable(this object checkObject)
{
bool isUsable = true;
if (checkObject == null || checkObject == DBNull.Value)
{
isUsable = false;
}
return isUsable;
}

EF: saveChanges succeed, but changes reflect in DB only when edditing existing reference member

I'm using EF.
I have a Parent entity named MamConfiguration_V1
it has a EntityCollection of MamConfigurationToBrowser_V1
my context is mMaMDBEntities.
I'm trying to add a new MamConfigurationToBrowser_V1 to an existing MamConfiguration_V1
I get no errors, but no record is added to the DB.
When I update an existing reference member, changes are reflected in the DB.
What am I doing wrong?
public MamConfiguration_V1 Save(MamConfiguration_V1 item)
{
try
{
var itemFromDB = mMaMDBEntities.MamConfiguration_V1.SingleOrDefault(a=> a.ConfigurationId == item.ConfigurationId);
if (itemFromDB != null)
{
UpdateEfBrowsers(itemFromDB, item);
mMaMDBEntities.SaveChanges();
return item;
}
else
{
throw new KeyNotFoundException(string.Format("configurationId = {0} wasn't found in the DB", item.ConfigurationId));
}
}
private void UpdateEfBrowsers(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi)
{
foreach (var item in itemFromDb.MamConfigurationToBrowser_V1.ToList())
{
if (itemFromUi.MamConfigurationToBrowser_V1.All(b => b.BrowserVersionId != item.BrowserVersionId))
{
mMaMDBEntities.MamConfigurationToBrowser_V1.DeleteObject(item);
}
}
for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
{
var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i);
var item =
itemFromDb.MamConfigurationToBrowser_V1.SingleOrDefault(b => b.BrowserVersionId == element.BrowserVersionId);
if (item != null)
{
// copy properties from element to item
}
//add new
else
{
//element.Browser = mMaMDBEntities.Browsers.Single(browserItem =>
// browserItem.BrowserID == element.BrowserID);
//element.MamConfigurationId = itemFromDb.ConfigurationId;
element.MamConfiguration_V1 = null;
mMaMDBEntities.MamConfigurationToBrowser_V1.AddObject(element);
itemFromDb.MamConfigurationToBrowser_V1.Add(element);
}
}
}
for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
{
...
mMaMDBEntities.MamConfigurationToBrowser_V1.AddObject(element);
itemFromDb.MamConfigurationToBrowser_V1.Add(element);
}
The problem was that the AddObject or Add
remove an item from itemFromUi.MamConfigurationToBrowser_V1.Count
and thus decreases count.
I don't know why.
I have replaced with foreach
You need to write
*databasename*.SaveChanges()
After you have added to the database. Otherwise it will not save it

The relationship could not be changed .. the foreign-key properties is non-nullable

I'm trying to update an entity of type MamConfiguration_V1
which exsit already in the DB
It has few reference members (among them MamConfigurationToBrowser_V1)
private void UpdateEfItem(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi)
{
itemFromDb.UpdatedDate = DateTime.Now;
itemFromDb.Description = itemFromUi.Description;
itemFromDb.StatusId = itemFromUi.StatusId;
itemFromDb.Name = itemFromUi.Name;
itemFromDb.NumericTraffic = itemFromUi.NumericTraffic;
itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic;
itemFromDb.Type = itemFromUi.NumericTraffic;
itemFromDb.MamConfigurationToBrowser_V1.Clear();
for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
{
var elementToAdd = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i);
elementToAdd.Browser = mMaMDBEntities.Browsers.Single(browserItem => browserItem.BrowserID == elementToAdd.BrowserID);
elementToAdd.MamConfiguration_V1 = itemFromDb;
itemFromDb.MamConfigurationToBrowser_V1.Add(elementToAdd);
}
}
I get the following DB runtime error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
But that's weird. All the references are not null:
Upadte 2:
I have tried this code:
try
{
item.ThrowIfNull("item");
var itemFromDB = GetById(item.ConfigurationId);
if (itemFromDB != null)
{
UpdateEfItem(itemFromDB, item);
//mMaMDBEntities.MamConfiguration_V1.Detach(itemFromDB);
//mMaMDBEntities.MamConfiguration_V1.Attach(item);
//mMaMDBEntities.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Modified);
//mMaMDBEntities.ObjectStateManager.ChangeObjectState(itemFromDB, System.Data.EntityState.Modified);
mMaMDBEntities.SaveChanges();
return item;
}
}
private void UpdateEfItem(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi)
{
itemFromDb.UpdatedDate = DateTime.Now;
itemFromDb.Description = itemFromUi.Description;
itemFromDb.StatusId = itemFromUi.StatusId;
itemFromDb.Name = itemFromUi.Name;
itemFromDb.NumericTraffic = itemFromUi.NumericTraffic;
itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic;
itemFromDb.Type = itemFromUi.NumericTraffic;
foreach (var item in itemFromDb.MamConfigurationToBrowser_V1.ToList())
{
if (itemFromUi.MamConfigurationToBrowser_V1.All(b => b.BrowserVersionId != item.BrowserVersionId))
{
mMaMDBEntities.MamConfigurationToBrowser_V1.DeleteObject(item);
}
}
for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
{
var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i);
var item = itemFromDb.MamConfigurationToBrowser_V1.SingleOrDefault(b => b.BrowserVersionId == element.BrowserVersionId);
if (item != null)
{
// copy properties from element to item
}
else
{
element.Browser = mMaMDBEntities.Browsers.Single(browserItem =>
browserItem.BrowserID == element.BrowserID);
element.MamConfiguration_V1 = itemFromDb;
//have also tried: element.MamConfiguration_V1 = null;
//element.MamConfiguration_V1Reference = null;
itemFromDb.MamConfigurationToBrowser_V1.Add(element);
}
}
}
and go this error:
{"Violation of UNIQUE KEY constraint 'UQ_MamConfigurations_V1'. Cannot
insert duplicate key in object 'dbo.MamConfiguration_V1'. The
duplicate key value is (elad_14Apr_1315).\r\nThe statement has been
terminated."}
This line...
itemFromDb.MamConfigurationToBrowser_V1.Clear();
...will not only clear the collection but also set the reference from the individual items in the collection to the parent itemFromDb to null. That's a modification of those items and EF will try to save them to the database. It fails because (probably) the reference is required and cannot be null.
You must take a different approach, that is updating the items in the collection one by one. You must take into account that items could have been deleted in the UI, that they could have been modified and that new items could have been added. It would look similar to this:
private void UpdateEfItem(MamConfiguration_V1 itemFromDb,
MamConfiguration_V1 itemFromUi)
{
itemFromDb.UpdatedDate = DateTime.Now;
itemFromDb.Description = itemFromUi.Description;
itemFromDb.StatusId = itemFromUi.StatusId;
itemFromDb.Name = itemFromUi.Name;
itemFromDb.NumericTraffic = itemFromUi.NumericTraffic;
itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic;
itemFromDb.Type = itemFromUi.NumericTraffic;
foreach (var item in itemFromDb.MamConfigurationToBrowser_V1.ToList())
{
if (!itemFromUi.MamConfigurationToBrowser_V1.Any(b =>
b.BrowserID == item.BrowserID)
{
mMaMDBEntities.Browsers.DeleteObject(item);
}
}
for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
{
var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i);
var item = itemFromDb.MamConfigurationToBrowser_V1
.SingleOrDefault(b => b.BrowserID == element.BrowserID);
if (item != null)
{
// copy properties from element to item
}
else
{
element.Browser = mMaMDBEntities.Browsers.Single(browserItem =>
browserItem.BrowserID == element.BrowserID);
itemFromDb.MamConfigurationToBrowser_V1.Add(element);
}
}
}

Categories

Resources