LINQ操做List

LINQ操做List<T>主要包括:spa

1.篩選code

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

2.拼接orm

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

3.分組統計blog

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.分組拼接get

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);
相關文章
相關標籤/搜索