从另一个集合更新集合的有效途径 [英] Efficient way of updating a collection from another collection

查看:126
本文介绍了从另一个集合更新集合的有效途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时更新的方式如下一的ObservableCollection 从另一个(都基于同一个类)足够好或更好,以另一种方式进行(或只是提高)?

 的foreach(myEntity所c在collection2)
    {
       collection1.Where(P => p.EntID == c.EntID).FirstOrDefault()字段1 = c.Field1。
       collection1.Where(P => p.EntID == c.EntID).FirstOrDefault()场2 = c.Field2。
       ...
       collection1.Where(P => p.EntID == c.EntID).FirstOrDefault()FieldN = c.FieldN。
    }
 

EntID 是主键。
(下够我的意思是快速而有效的好)。

解决方案

  VAR myItem = collection1.Where(P => p.EntID == c.EntID).FirstOrDefault() ;
   如果(myItem == NULL)
       继续;
   myItem.Field1 = c.Field1;
   myItem.Field2 = c.Field2;
   ...
   myItem.FieldN = c.FieldN;
 

如果 myItem C 是不同的类型,看看AutoMapper。

Is the following way of updating an ObservableCollection from another one (both based on the same class) good enough or it better to be done in another way (or just to be improved)?

foreach (MyEntity c in collection2)
    {
       collection1.Where(p => p.EntID == c.EntID).FirstOrDefault().Field1 = c.Field1;
       collection1.Where(p => p.EntID == c.EntID).FirstOrDefault().Field2 = c.Field2;
       ...
       collection1.Where(p => p.EntID == c.EntID).FirstOrDefault().FieldN = c.FieldN;         
    }

EntID is the primary key.
(Under good enough I mean fast and efficient).

解决方案

   var myItem = collection1.Where(p => p.EntID == c.EntID).FirstOrDefault();
   if (myItem == null)
       continue;
   myItem.Field1 = c.Field1;
   myItem.Field2 = c.Field2;
   ...
   myItem.FieldN = c.FieldN;

If myItem and c are different types, have a look at AutoMapper.

这篇关于从另一个集合更新集合的有效途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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