将填充的POCO合并为单个POCO

编程入门 行业动态 更新时间:2024-10-26 06:37:20
本文介绍了将填充的POCO合并为单个POCO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

// 只是我所拥有的一个例子 产品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方法吗? 到目前为止我所拥有的:

// 只是一个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

本文发布于:2023-07-27 11:48:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222290.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并为   POCO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!