如何使用LINQ在字符串的一部分上创建分组?

时间:2019-05-31 04:12:21

标签: c#

我有一个按UtcNow排序的ViewsHistory对象列表:

protected void Application_Start()
        {
            Application["SiteVisitedCounter"] = 0;
            Application["OnlineUserCounter"] = 0;
        }
                public void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application.Lock();
            Application["SiteVisitedCounter"] = Convert.ToInt32(Application["SiteVisitedCounter"]) + 1;
            Application["OnlineUserCounter"] = Convert.ToInt32(Application["OnlineUserCounter"]) + 1;
            Application.UnLock();
        }
        public void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            Application["OnlineUserCounter"]= Convert.ToInt32(Application["OnlineUserCounter"]) - 1;
            Application.UnLock();
        }

ViewHistory类:

<form name="myForm" onchange="event.preventDefault(); calcForm2(this);">...radiobuttons...</form>

有没有一种使用LINQ的方法,我可以创建一个新列表来汇总YYMM的学习,练习和测验总数?

2 个答案:

答案 0 :(得分:1)

您可以使用SELECT P.IDProduct,P.ProductName,P.FK_Category,C.CategoryName FROM Product P inner join Category C on P.FK_Category=C.IDCategory and p.FK_User=C.Fk_User WHERE P.FK_User=1 LINQ表达式来实现:

GroupBy

结果

 var views = new List<ViewHistory>();
            views.Add(new ViewHistory {YYMMDD = "190530", Learn = 20, Practice = 1, Quiz = 30});
            views.Add(new ViewHistory { YYMMDD = "190531", Learn = 1, Practice = 1, Quiz = 30 });
            views.Add(new ViewHistory { YYMMDD = "190529", Learn = 2, Practice = 1, Quiz = 30 });
            views.Add(new ViewHistory { YYMMDD = "190410", Learn = 2, Practice = 1, Quiz = 30 });
            var newViews = views.GroupBy(t => t.YYMMDD.Substring(0, 4))
                .Select(t => new
                {
                    yymm = t.Key,
                    learnSum = t.Select(f => f.Learn).Sum(),
                    practiceSum = t.Select(f => f.Practice).Sum(),
                    quizSum = t.Select(f => f.Quiz).Sum()
                }).ToList();

答案 1 :(得分:1)

解决方案

var views = new List<ViewHistory>
{
    new ViewHistory {YYMMDD = "190515", Learn = 1, Practice = 2, Quiz = 3},
    new ViewHistory {YYMMDD = "190514", Learn = 5, Practice = 6, Quiz = 7},
    new ViewHistory {YYMMDD = "190415", Learn = 100, Practice = 110, Quiz = 120},
    new ViewHistory {YYMMDD = "190414", Learn = 150, Practice = 160, Quiz = 170}
};

var monthlyTotals = views.GroupBy(
    v => v.YYMMDD.Substring(0, 4),
    v => v,
    (yearMonth, groupedViews) =>
        new
        {
            YearMonth = yearMonth,
            LearnSum = groupedViews.Sum(view => view.Learn),
            PracticeSum = groupedViews.Sum(view => view.Practice),
            QuizSum = groupedViews.Sum(view => view.Quiz)
        }).ToList();

结果

monthlyTotals (调试时从Visual Studio本地窗口复制)

monthlyTotals   Count = 2   System.Collections.Generic.List<<>f__AnonymousType0<string, int, int, int>>
    [0] { YearMonth = "1905", LearnSum = 6, PracticeSum = 8, QuizSum = 10 } <Anonymous Type>
    [1] { YearMonth = "1904", LearnSum = 250, PracticeSum = 270, QuizSum = 290 } <Anonymous Type>

宁愿不使用匿名类型?

当然,如果您不想使用匿名类型,则可以定义一个新类(例如,MonthlyViewTotals)并实例化monthlyTotals的类列表,而不是像这个:

var monthlyTotals = views.GroupBy(
    v => v.YYMMDD.Substring(0, 4),
    v => v,
    (yearMonth, groupedViews) =>
        new MonthlyViewTotals        // <-- no more anonymous types
        {
            YearMonth = yearMonth,
            LearnSum = groupedViews.Sum(view => view.Learn),
            PracticeSum = groupedViews.Sum(view => view.Practice),
            QuizSum = groupedViews.Sum(view => view.Quiz)
        }).ToList();