comparing two List

i have gridview control with a checkbox on it

When i hit on save button i able to find the checkbox which have been checked and i able to do it so far so good but the problem is:

let say if the user tries to uncheck the checkedbox so how would i track the changes and save it into the db that has been checked. anyhelp?.. so in that regards i have created two list for comparision… hope i make sense here.

i want to compare the two list and if any changes then save else ….do something…

<asp:TemplateField HeaderText="Select">  <ItemTemplate>  
      <asp:CheckBox ID="chkSelected" runat="server" Checked="false"></asp:CheckBox>   
</ItemTemplate>  </asp:TemplateField>     


List<Employee> listFromDB = new List<Employee>();
listFromDB = EmployeeListFromDB ; //loads the list

List<Employee> selectedEmployee = new List<Employee>();
selectedEmployee = MySelectedEmployee //loads the list

//Employee object looks like this:
id
name

here is what i got stuck and here is what i am doing…

foreach (Employee item in MySelectedEmployee )
{
    bool _flag = false;
    _flag = EmployeeService.SaveEmployee(item.Id, item.Name);
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You want something like this:

var diff = selectedEmployee.Except(listFromDb, (a,b)=>a.id==b.id);
foreach (Employee e in diff)
{
    EmployeeService.SaveEmployee(e.Id, e.Name);
}

but you’re awful short on particulars. What defines a change? How will match items in the list: by id? Can you be more exact with your requirements?

Method 2

Something like this should work:

foreach (Employee item in MySelectedEmployee )
{
   var currentEntry = listFromDB.FirstOrDefault( x => x.id == item.id);
   if(currentEntry == null || currentEntry.name!= item.name)
   {
     bool _flag = EmployeeService.SaveEmployee(item.Id, item.Name);
    ...
   }
}

Note that your initializations of List<Employee> are unnecessary, since you re-assign the variables on the next lines anyway.

Method 3

It sounds like what you need is set operations. You have a few choices:

.NET 4.0 – HashSet<T>

http://msdn.microsoft.com/en-us/library/bb359438.aspx

.NET 3.5 – Linq extensions

The Linq to Objects extensions contains some set operations like Intersect and Union.
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx

.NET 2.0 – SetList<T>

In 2.0 you’ll need to hand-roll something like the following SetList class.
http://csharptest.net/browse/src/Library/Collections/SetList.cs
(online-help)

To use SetList:

SetList<Employee> listFromDB = new SetList<Employee>(EmployeeListFromDB);
SetList<Employee> selectedEmployee = new SetList<Employee>(MySelectedEmployee);

SetList<Employee> removed = listFromDB.SubtractSet(selectedEmployee);
SetList<Employee> added = selectedEmployee.SubtractSet(listFromDB);

Note: Employee must implement IComparable<Employee>, or you should write an IComparer<Employee> and provide it to the SetList constructor calls above.

Method 4

here is how you would do:

List<Employee> found = EmployeeListFromDB.FindAll(a=>!selectedEmployee.Exists(b=>a.Id == b.Id));

thats it…


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x