您的位置:首页 > 其它

LINQ操作List<T>

2015-07-20 14:12 295 查看
LINQ操作List<T>主要包括:

1.筛选

List<string> stcdList = stcdArray.ToList<string>()
.FindAll(new Predicate<string>(stcd => stcd.Contains("stcd_")))
.Select(stcd => stcd.Substring(5))
.ToList<string>();


2.拼接

List<string> suggestion = new List<string>();
string sg_font_details = suggestion.Aggregate((s1, s2) => string.Format("{0},{1}", s1, s2)).ToString();


3.分组统计

public class HomeStat
{
public string adnm { get; set; }
public string ProCount { get; set; }
public string countCompare { get; set; }
public string perCount { get; set; }
public string dayCount { get; set; }
public string perDayCount { get; set; }
}
List<HomeStat> chsList = new List<HomeStat>() {... };
var query = hsList.GroupBy(t => t.adnm).Select(p => new
{
adnm = p.Key,
dayCount = p.Sum(x => double.Parse(x.dayCount)),
proCount = p.Select(x => x.projectID).Distinct().Count(),
perCount = p.Sum(x => double.Parse(x.perCount)),
perDayCount = p.Sum(x => double.Parse(x.perDayCount))
});


4.分组拼接

public class Service
{
public string Month { get; set; }
public string Content { get; set; }
}
List<Service> serList = new List<Service>(){...};
var query = serList.GroupBy(t => t.Month).Select(g => g.Aggregate((s1, s2) =>
new Service { Month = g.Key, Content = s1.Content + "," + s2.Content }))
.OrderByDescending(t => t.Month);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: