将填充的POCO合并为单个POCO [英] Consolidate populated POCOs into a single POCO

查看:76
本文介绍了将填充的POCO合并为单个POCO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一系列产品POCO。填充了一些数据,可用于识别独特的产品。其他字段,例如PriceA和PriceB被分成几个实例:

  //  < span class =code-comment>只是我所拥有的一个例子 
产品A1 = new 产品{Name = A,PriceA = 1 29 ,PriceB = 0 ,Desc = null };
产品A2 = new 产品{Name = A,PriceA = 0 ,PriceB = 1 39 ,Desc = null };
产品A2 = new 产品{Name = A,PriceA = 0 ,PriceB = 0 ,Desc = ProductA};



我希望能够将这些合并为一个:

产品A1 = 产品{Name =  ,PriceA =  1  29 ,PriceB =  1  39 ,Desc =   ProductA}; 





有一种聪明的Linq方法吗?



到目前为止我所拥有的:

 < span class =code-comment> //  只是一个mo ck up。我真正的GroupBy产品有大约10个标识属性和两倍的细节要合并: 
var products = ImportProducts();

var groups = products.GroupBy(p => new Product {Name = p.Name});

// 然后我想我可以从每个项目中获取值?

产品组。选择(g = > {
var result = g.Key;
result.PriceA = g.Max(p => p.PriceA);
result.PriceB = g.Max(p => p.PriceB);
result.Desc = g.FirstOrDefault(p =>!string.isNullorEmpty(p.Desc))。Desc;
return result;
});





还有更好的办法吗?



谢谢

Andy

解决方案

嗯......你可以作弊! :笑:

产品A1 = 产品{Name =   A,PriceA =  1  29 ,PriceB =  0 ,Desc =  null }; 
产品A2 = new 产品{Name = A,PriceA = 0 ,PriceB = 1 39 ,Desc = null };
产品A3 = new 产品{Name = A,PriceA = 0 ,PriceB = 0 ,Desc = ProductA};
var products = new 列表< Product>(){A1,A2,A3};
var groups = products.GroupBy(p = > p.Name)
。选择(g = > new 产品{Name = g.Key,
PriceA = g.Max(v = > v.PriceA),
PriceB = g.Max(v = > v.PriceB),
Desc = g.Max(v = > v.Desc)});


Hi,

I have an array of product POCOs. Some data is populated and can be used to identify unique products. Other fields, such as PriceA and PriceB are slit out into a couple of instances:

//Just an example of what I have
Product A1 = new Product{Name = "A", PriceA = 1.29, PriceB = 0, Desc = null};
Product A2 = new Product{Name = "A", PriceA = 0, PriceB = 1.39, Desc = null};
Product A2 = new Product{Name = "A", PriceA = 0, PriceB = 0, Desc = "ProductA"};


I want to be able to consolidate these into one:

Product A1 = new Product{Name = "A", PriceA = 1.29, PriceB = 1.39, Desc = "ProductA"};



Is there a clever Linq way to do this?

What I have so far:

//Just a mock up.  My real GroupBy Product has about 10 identifying properties and twice as many details to consolidate:
var products = ImportProducts();

var groups = products.GroupBy(p=>new Product{Name=p.Name});

//Then I guess I could get the values from each item?

products groups.Select(g=>{
  var result = g.Key;
  result.PriceA = g.Max(p=>p.PriceA);
  result.PriceB = g.Max(p=>p.PriceB);
  result.Desc = g.FirstOrDefault(p=>!string.isNullorEmpty(p.Desc)).Desc;
  return result;
});



Is there a better way?

Thanks
Andy

解决方案

Um...you could cheat! :laugh:

Product A1 = new Product { Name = "A", PriceA = 1.29, PriceB = 0, Desc = null };
Product A2 = new Product { Name = "A", PriceA = 0, PriceB = 1.39, Desc = null };
Product A3 = new Product { Name = "A", PriceA = 0, PriceB = 0, Desc = "ProductA" };
var products = new List<Product>() { A1, A2, A3 };
var groups = products.GroupBy(p => p.Name)
                     .Select(g => new Product { Name = g.Key,
                                                PriceA = g.Max(v => v.PriceA),
                                                PriceB = g.Max(v => v.PriceB),
                                                Desc = g.Max(v => v.Desc) });


这篇关于将填充的POCO合并为单个POCO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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