开发者

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 H开发者_运维百科eaderText="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);
}


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?


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.


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.


here is how you would do:

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

thats it...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜