如何通过枚举和日期对LINQ to Entities结果进行分组?

时间:2016-12-02 02:54:36

标签: c# linq group-by

请允许我事先承认我可能没有正确地提出这个问题,因为我发现你无法在GroupBy值上使用DateTime,但我是我已经在Objective-C工作了四年,而且我还没有完全使用过LINQ。这是我目前的代码:

var filteredEvents = events.Where(e => e.EventDate < DateTimeOffset.Now.AddMonths(1)).GroupBy(e => e.EventStatusKey);

在此代码中,events是匿名类型的IEnumerable,由四个字段组成,包括EventDateDateTime)和EventStatusKey({ {1}})。

目前,上述代码会针对事件状态enumReady返回两个组。但是,我还需要根据日期细分InProgress个事件。具体来说,我需要针对Ready个事件的单独组,其中:

Ready

EventDate >= DateTime.Date.Today

1 个答案:

答案 0 :(得分:0)

当然,你可以这样做:

.testWrapOverflowFade {
  overflow: hidden;
  position: relative;
  height: 2.4em; /* NOL = height in this class/height in after class. Here it is 2.4/1.2 = 2. Change it to 3.6em for display 3 lines. */
}
.testWrapOverflowFade:after {    
    content: "";
    text-align: right;
    position: absolute;
    bottom: 0;
    right: 0;
    width: 10%;
    height: 1.2em;
  background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
  background: -moz-linear-gradient(left, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
  background: -o-linear-gradient(left, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
  background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
}

<div class="testWrapOverflowFade">This is a long text this is a long text 2  this is a long text 3  this is a long text 4  this is a long text 5  this is a long text6  this is a long text7  this is a long text 8  this is a long text 9  this is a long text 10  this is a long text 11  this is a long text12 </div>

这将分组所有状态以及是否在今天之前。然后你可以像这样折叠组(例如当它的InProgress时):

var filteredEvents = 
    events.Where(e => e.EventDate < DateTimeOffset.Now.AddMonths(1))
    .GroupBy(e => new {
        e.EventStatusKey,
        BeforeToday = e.EventDate < DateTime.Today
    });

这将使团体变得扁平化,基本上忽略了“今天之前”。标志。