自定义对象的组列表,如果count> 1,则返回 [英] Group list of custom objects and return if count >1
本文介绍了自定义对象的组列表,如果count> 1,则返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友我需要根据字段 Cusip
对对象列表进行分组。并且需要返回有计数的新列表> 1
请帮助
public class AccruedClass
{
public string Cusip { get ; set ; }
public string 符号{ get 跨度>; set ; }
public double AccruedInterest { get 跨度>; set ; }
public double PriceFactor { get 跨度>; set ; }
public double 数量{ get 跨度>; set ; }
public double CalcAcc { get { return AccruedInterest * PriceFactor * Quantity; }
}
列表< AccruedClass> ac = new 列表< AccruedClass>();
ac = validaccrued();
ac.ForEach(item = > Console.Write(item.Cusip + < span class =code-string>, + item.AccruedInterest + , + item.Quantity + \ n));
解决方案
分组很简单:
var grouped = ac.GroupBy(a = > a.Cusip);但是你只想要那些拥有多个成员的组? br />
试试这个:
List< AccruedClass> grouped = ac.GroupBy(a = > a.Cusip)。其中(g = > g .Count()> 1 )。SelectMany(g = > g.ToList())。ToList();
忘记编码HTML标记字符... [/编辑]
Hi Friends i need to group a list of objects based on a field Cusip
. and need to return new list which has count>1
Please help
public class AccruedClass
{
public string Cusip { get; set; }
public string Symbol { get; set; }
public double AccruedInterest { get; set; }
public double PriceFactor { get; set; }
public double Quantity { get; set; }
public double CalcAcc { get { return AccruedInterest * PriceFactor * Quantity; } }
}
List<AccruedClass> ac = new List<AccruedClass>();
ac = validaccrued();
ac.ForEach(item => Console.Write(item.Cusip + "," + item.AccruedInterest + "," + item.Quantity + "\n"));
解决方案
Grouping is easy:
var grouped = ac.GroupBy(a => a.Cusip);But you want only those groups with more than one member?
Try this:
List<AccruedClass> grouped = ac.GroupBy(a => a.Cusip).Where(g => g.Count() > 1).SelectMany(g => g.ToList()).ToList();
[edit]Forgot to encode HTML tag characters...[/edit]
这篇关于自定义对象的组列表,如果count> 1,则返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文