比较两个List<> [英] comparing two List<>

查看:179
本文介绍了比较两个List<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有上有一个复选框GridView控件

当我打的保存按钮我能找到已检查的复选框,我能够做到这一点到目前为止好,但问题是:

如果让用户尝试取消选中checkedbox所以如何将我跟踪更改,并保存到已检查了分贝说。 anyhelp?..所以在这方面我已经创建了两个名单对比...希望我意义在这里。

我想比较两个列表,如果任何更改,然后保存别的....做些什么...

 < ASP:的TemplateField的HeaderText =选择> <&ItemTemplate中GT;
      < ASP:复选框ID =chkSelected=服务器选中=假>< / ASP:复选框>
< / ItemTemplate中> < / ASP:的TemplateField>
清单<员工> listFromDB =新的List<员工>();
listFromDB = EmployeeListFromDB; //加载列表清单<员工> selectedEmployee =新的List<员工>();
selectedEmployee = MySelectedEmployee //加载列表// Employee对象如下:
ID
名称

这里是我被困,这里是我在做什么...

 的foreach(在MySelectedEmployee员工项)
{
    布尔_flag = FALSE;
    _flag = EmployeeService.SaveEmployee(item.Id,item.Name);
}


解决方案

这里是你会怎么做:

 列表<员工>找到= EmployeeListFromDB.FindAll(A =>!selectedEmployee.Exists(B = GT; a.Id == b.Id));

完蛋了...

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);
}

解决方案

here is how you would do:

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

thats it...

这篇关于比较两个List&LT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆