組後,保留財產我有用途的集合,如下所示,需要下面的輸出如下:無法通過LINQ
這是我的用法類:
public class Usage
{
public Usage() {}
public string Alias { get; set; }
public int ActivityCount { get; set; }
public DateTime HitDate { get; set; }
}
這是我的代碼:
我想先集合到用戶和每個用戶,我想要得到每天的點擊數,如下面的預期輸出中所示。請注意,日期時間a和d是同一天的不同時間,因此需要將它們組合在一起。
此我將用它來繪製成命中條形圖VS日期即每天
擊中這樣做,我無法別名字段的數據保存到我的第二個級別分組:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NullOperator
{
class Program
{
static void Main(string[] args)
{
var a = new DateTime(2015, 10, 23, 12, 05, 06);
var b = new DateTime(2015, 10, 24, 12, 05, 06);
var c = new DateTime(2015, 10, 25, 12, 05, 06);
var d = new DateTime(2015, 10, 23, 14, 05, 06);
var usages = new List<Usage>
{
new Usage{Alias = "A ",HitDate = a},
new Usage{Alias = "A ",HitDate = a},
new Usage{Alias = "A ",HitDate = d},
new Usage{Alias = "A ",HitDate = b},
new Usage{Alias = "A ",HitDate = b},
new Usage{Alias = "A ",HitDate = b},
new Usage{Alias = "A ",HitDate = c},
new Usage{Alias = "B ",HitDate = a},
new Usage{Alias = "B ",HitDate = b},
new Usage{Alias = "B ",HitDate = b},
new Usage{Alias = "B ",HitDate = c},
new Usage{Alias = "B ",HitDate = c},
new Usage{Alias = "B ",HitDate = c},
new Usage{Alias = "C ",HitDate = a},
new Usage{Alias = "C ",HitDate = a},
new Usage{Alias = "C ",HitDate = a},
new Usage{Alias = "C ",HitDate = c},
new Usage{Alias = "C ",HitDate = c},
new Usage{Alias = "C ",HitDate = c},
};
var g = usages.GroupBy(r => r.Alias);
foreach (var u in g)
{
var x = u.GroupBy(r => r.HitDate.Day).Select(gr=>new
{
HitDate=gr.Key,
Count=gr.Count()
});
foreach (var m in x)
{
Console.WriteLine(m.HitDate + " " +m.Count);
}
}
Console.Read();
}
}
//預期輸出
Alias : HitDate.Date : hitcount
// A - 23/10/2015 - 3
// A - 24/10/2015 - 3
// A - 25/10/2015 - 1
// B - 23/10/2015 - 1
// B - 24/10/2015 - 2
// B - 25/10/2015 - 3
// C - 23/10/2015 - 3
// C - 24/10/2015- 0
// C - 25/10/2015 - 3
下的輸出:
23 3
24 3
25 1
23 1
24 2
25 3
23 3
25 3
正如你所看到的,分組是可以的,但像整個Date和Alias這樣的信息會丟失,並且無法在嵌套循環的代碼中檢索它們。
對此有何建議?關於如何獲得所需的輸出?
除了別的,現在是瞭解自動實現屬性的好時機:更簡單地通過一氣呵成雖然別名和日期分組做到這一點長... –
我知道先生。我從需要Property更改的更大應用程序複製此代碼。我刪除了OnPropertyChanegd方法,但忘記在這裏刪除它。讓我更新。 –