LINQ操作List<T>主要包括:
1.筛选
List<string> stcdList = stcdArray.ToList<string>()
.FindAll(new Predicate<string>(stcd => stcd.Contains("stcd_")))
.Select(stcd => stcd.Substring())
.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);