Group By Contains [Date]

时间:2016-02-12 20:09:19

标签: c# linq datetime group-by

I have a List where I make a group by on date values, and I would like to make an if statement on the group by item containing a DateNow, but I cant find it to work..

This is a sample of my Group By:

@foreach (var item in Model.Select(v => new //here you count your total
    {
        Fecha = v.Fecha,
        Total = v.Total * v.Cantidad
    })
    .GroupBy(l => l.Fecha) //and then grouping
    .Select(z => new
    {
        Dia = z.Key,
        Total = z.Sum(l => l.Total)
    }))
{
    if (item.ToString().Contains(date))
    {  
        <input value="@item" />
    }
}

And it give me this Output:

{ Dia = 1/1/2016 12:00:00 AM, Total = 40293.0000 }
{ Dia = 1/3/2016 12:00:00 AM, Total = 96774.0000 }
{ Dia = 2/12/2016 12:00:00 AM, Total = 34374.0000 }

and I want a if statement that only give me the group by of Today in the example above I would like the if to only give me this output:

{ Dia = 2/12/2016 12:00:00 AM, Total = 34374.0000 }

but my "if (item.ToString().Contains(date))" don't work. What I am doing wrong?

2 个答案:

答案 0 :(得分:3)

You can add Where() part to get only records agains today like:

.GroupBy(l => l.Fecha)
.Where(x=> x.Key.Date == DateTime.Now.Date)
.......................
.......................

or:

if (item.Dia.Date == DateTime.Today)
{
   <input value="@item" />
} 

答案 1 :(得分:0)

You can use a where statement like this:

degrees/radians mode