使用LINQ to entity在内存中创建表

时间:2015-09-16 21:48:59

标签: c# entity-framework linq

我有这张桌子(A): enter image description here

在LINQ的帮助下,我想从上面的SQL表(B)中在内存中创建这个表:

enter image description here

这是我的实体:

    private class HelperClass
    {
        public string clientName { get; set; }
        public string authority { get; set; }
        public int objectzsNumber { get; set; }
        public int month { get; set; }
        public int semiannual { get; set; }
        public int quarterly { get; set; }
        public int yearly { get; set; }
    }

任何想法如何使用 LINQ to entity 从表A在内存中创建表B?

1 个答案:

答案 0 :(得分:2)

您需要的只是GroupBy。当你在内存中说时,它意味着应该使用Linq To Object,但是它的语法并不重要(它们都是相同的,首先要在内存对象中调用ToList()):< / p>

//suppose your A table is mapped to clients
var result = clients.GroupBy(e=> new { e.ClientName, e.Authority, 
                                       e.ObjectsNumber, e.Frequency})
                    .Select(g => new HelperClass{
                         clientName = g.Key.ClientName,
                         authority = g.Key.Authority,
                         objectsNumber = g.Key.ObjectsNumber,
                         monthly = g.Key.Frequency == "Monthly" ? g.Count() : 0,
                         semiannual = g.Key.Frequency == "SemiAnnual" ? g.Count() : 0,
                         quaterly = g.Key.Frequency == "Quaterly" ? g.Count() : 0,
                         yearly = g.Key.Frequency == "Yearly" ? g.Count() : 0
                     });