Compare values obtained from the same query

1

I'm new to C #, I'm doing a web application in VisualStudio 2015, with ASP.NET MVC5 and EF6. I need to compare the values that a linq query throws at me, normally I would do it with a two for , one within the other, although maybe they can suggest a more optimal way to do it. My problem is that when I do the for I have no way to get the value I need to compare.

Controller

var query_y_axis = db.registro.Select(o => new { o.value, o.date});
var minutes = 0;

for(var i = 0; i < query_y_axis .LongCount()-1; i++)
{
    minutes = db.registro.  //AQUI NO OBTENGO EL CAMPO POR EL QUE DESEO COMPARAR QUE ES DATE
}

My intention is to obtain the date field ( date ), and in minutes store the minute of that date, and go comparing with the other data of that same query. and In the end get only the values that are in the same minute.

For example, if I get a list

  

2018-02-13 10: 04: 01,2018-02-13 10:04:02, 2018-02-13 10:04:50, 2018-02-13 10:04:59, 2018-02-13 10:05:06, 2018-02-13 10:05:17, 2018-02-13 10:05:23

of this list would be comparing the first with the others and I would be later another list with the first 4 values that are those that have the same minute as the initial date

  

(2018-02-13 10:04:01, 2018-02-13 10:04:02, 2018-02-13 10:04:50,   2018-02-13 10:04:59)

Thanks in advance.

    
asked by Mary 15.03.2018 в 15:18
source

1 answer

1

You have to use query_y_axis , which is the result of your query. Use a foreach instead of a for , so you can access the properties you have defined:

foreach (var reg in query_y_axis)
{
    minutes=reg.date;
}

Edit

For the second part of your question, it is not really necessary to use a loop, for that we have LINQ to the rescue. Although you talk about the same minute, I imagine that it must also be the same date and time. Simply, we filter the results with Where and compare the date, hour and minutes of all the elements with the date, time and minutes of the first element:

var query_y_axis = db.registro.Select(o => new { o.value, o.date});
var mismominuto = query_y_axis.Where(x => x.date.Date== query_y_axis.First().date.Date &&
                                          x.date.Hour == query_y_axis.First().date.Hour &&
                                          x.date.Minute == query_y_axis.First().date.Minute);
    
answered by 15.03.2018 в 15:23