Skip to content

Latest commit

 

History

History
115 lines (97 loc) · 12.2 KB

README.md

File metadata and controls

115 lines (97 loc) · 12.2 KB

Applied

.NET Extension methods for object-object or dictionary-object or datatable-object mapping, single item mapping use [ item.Apply(()=>new { .. }); ],multiple array items mapping use [ items.Apply(a=>new { .. }); ].
Extension methods for SQL Window Function in Linq, use [ items.GroupBy(a =>...).AsPartition(p=> p.OrderBy(a=>...)).Over(p=>...); ],can also be customize function.

NuGet version (Applied)

    public enum UserEnum
    {
        None,
        User
    }
    [Serializable]
    public class User
    {
        public int UserID { get; set; }
        public string Name { get; set; }
        public DateTime? Time { get; set; }
        public UserEnum Enum { get; set; }
    }
    [Serializable]
    public class UserViewModel
    {
        public int UserID { get; set; }
        public string Name { get; set; }
        public DateTime? Time { get; set; }
        public UserEnum Enum { get; set; }
    }
    User[] users = new User[]
    {
        new User() { UserID = 1, Name = "Sam     " },
        new User() { UserID = 2, Name = "John    " }
    };

    users.Apply(a => new { Time = DateTime.Now, Enum = UserEnum.User });
    users.Trim();
    users.Apply(a => new { Time = DateTime.Now, Enum = UserEnum.User }).Trim();
    UserViewModel[] vm1 = users.ToDataEnumerable<User, UserViewModel>().ToArray();
    DataTable dt1 = users.ToDataTable();
    Dictionary<string, object>[] ds1 = users.ToDictionaries().ToArray();

    UserViewModel[] vm2 = dt1.ToDataEnumerable<UserViewModel>().ToArray();
    UserViewModel[] vm3 = ds1.ToDataEnumerable<UserViewModel>().ToArray();

    DataTable dt2 = vm1.ToDataTable();
    DataTable dt3 = ds1.ToDataTable();

    Dictionary<string, object>[] ds2 = vm1.ToDictionaries().ToArray();
    Dictionary<string, object>[] ds3 = dt1.ToDictionaries().ToArray();
    public class TestData
    {
        public int Year { get; set; }
        public string Name { get; set; }
        public decimal Value { get; set; }
        public decimal Sum { get; set; }
        public decimal Average { get; set; }
        public int RowNumber { get; set; }
        public int Ntile { get; set; }
        public int DenseRank { get; set; }
        public int Rank { get; set; }
        public decimal FirstValue { get; set; }
        public decimal LastValue { get; set; }
        public decimal NthValue { get; set; }
        public decimal Lead { get; set; }
        public decimal Lag { get; set; }
        public decimal CumeDist { get; set; }
        public decimal PercentRank { get; set; }
        public decimal PercentileDisc { get; set; }
        public decimal PercentileCont { get; set; }
        public decimal KeepDenseRankFirst { get; set; }
        public decimal KeepDenseRankLast { get; set; }
    }
    List data = new List();
    data.Add(new TestData() { Year = 2019, Name = "A", Value = 111.1m });
    data.Add(new TestData() { Year = 2019, Name = "B", Value = 333.3m });
    data.Add(new TestData() { Year = 2019, Name = "C", Value = 333.3m });
    data.Add(new TestData() { Year = 2019, Name = "A", Value = 222.2m });
    data.Add(new TestData() { Year = 2019, Name = "C", Value = 444.4m });
    data.Add(new TestData() { Year = 2019, Name = "A", Value = 222.2m });
    data.Add(new TestData() { Year = 2019, Name = "B", Value = 333.3m });
    data.Add(new TestData() { Year = 2019, Name = "C", Value = 555.5m });
    data.Add(new TestData() { Year = 2020, Name = "A", Value = 111.1m });
    data.Add(new TestData() { Year = 2020, Name = "B", Value = 333.3m });
    data.Add(new TestData() { Year = 2020, Name = "A", Value = 222.2m });
    data.Add(new TestData() { Year = 2020, Name = "C", Value = 333.3m });

    data = data.GroupBy(a => new { a.Year }).AsPartition(p => p.OrderBy(a => a.Value).ThenBy(a => a.Name))
    .Over(p => p.Sum(a => a.Value), a => a.Sum)
    .Over(p => p.Average(a => a.Value), a => a.Average)
    .Over(p => p.RowNumber(), a => a.RowNumber)
    .Over(p => p.Ntile(2), a => a.Ntile)
    .Over(p => p.DenseRank(), a => a.DenseRank)
    .Over(p => p.Rank(), a => a.Rank)
    .Over(p => p.FirstValue(a => a.Value), a => a.FirstValue)
    .Over(p => p.LastValue(a => a.Value), a => a.LastValue)
    .Over(p => p.NthValue(a => a.Value, 2), a => a.NthValue)
    .Over(p => p.Lead(a => a.Value), a => a.Lead)
    .Over(p => p.Lag(a => a.Value), a => a.Lag)
    .Over(p => p.CumeDist(), a => a.CumeDist)
    .Over(p => p.PercentRank(), a => a.PercentRank)
    .Over(p => p.PercentileDisc(0.5m, a => a.Value), a => a.PercentileDisc)
    .Over(p => p.PercentileCont(0.5m, a => a.Value), a => a.PercentileCont)
    .Over(p => p.KeepDenseRankFirst(g => g.Sum(a => a.Value)), a => a.KeepDenseRankFirst)
    .Over(p => p.KeepDenseRankLast(g => g.Sum(a => a.Value)), a => a.KeepDenseRankLast)
    .ToList();