0

I have table class Logging

Here is code:

public partial class Logging
{
    public string Imei { get; set; }
    public DateTime CurDateTime { get; set; }
    public Nullable<System.DateTime> GPSDateTime2 { get; set; }
    public Nullable<decimal> Latitude2 { get; set; }
    public Nullable<decimal> Longitude2 { get; set; }
    public int Speed { get; set; }
    public Nullable<int> Datatype { get; set; }
    public int Id { get; set; }

}

Also I have ViewModel

 public class HeatmapViewModel
{
    public decimal? Latitude2 { get; set; }
    public decimal? Longitude2 { get; set; }
    public int FirstStartDifference { get; set; }
    public int LastStartDifference { get; set; }
    public int coeff = 2;
    public int Difference;
}

I have method in repository where I do all calculations

Here is code

var allitems = ctx.Loggings.AsEnumerable().Select(
                x => new Logging
                {
                    Longitude2 = x.Longitude2,
                    Latitude2 = x.Latitude2,
                    CurDateTime = x.CurDateTime,
                    Datatype = x.Datatype

                });
            var filteredQuery = allitems.Where(x => x.Datatype == 1 || x.Datatype == 2).OrderByDescending(x => x.Id).ToList();
            for (int i = 1; i < filteredQuery.Count; i++)
            {
                if (filteredQuery[i].Datatype == 2 && filteredQuery[i - 1].Datatype == 1)
                {
                    TimeSpan differenceTicks = filteredQuery[i].CurDateTime - filteredQuery[i - 1].CurDateTime;
                    var differenceInMinutes = (int) differenceTicks.TotalMinutes;

                }
            }

            items.Add(new HeatmapViewModel
            {
                Latitude2 = allitems.Longitude2,
                Longitude2 = allitems.Longitude2,
                Difference = differenceInMinutes
            });

I have trouble with this block of code:

  items.Add(new HeatmapViewModel
            {
                Latitude2 = allitems.Longitude2,
                Longitude2 = allitems.Longitude2,
                Difference = differenceInMinutes
            });

Here is errors:

Severity Code Description Project File Line Suppression State Error CS1061 'IEnumerable' does not contain a definition for 'Longitude2' and no extension method 'Longitude2' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?) Heatmap C:\Users\nemes\source\repos\Heatmap\Heatmap\Repository\HeatmapRepository.cs 91 Active

Severity Code Description Project File Line Suppression State Error CS1061 'IEnumerable' does not contain a definition for 'Longitude2' and no extension method 'Longitude2' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?) Heatmap C:\Users\nemes\source\repos\Heatmap\Heatmap\Repository\HeatmapRepository.cs 92 Active Severity Code Description Project File Line Suppression State Error CS0103 The name 'differenceInMinutes' does not exist in the current context Heatmap C:\Users\nemes\source\repos\Heatmap\Heatmap\Repository\HeatmapRepository.cs 93 Active

How I can solve them?

4
  • Why in the world do you have that first query - replace it with just var allitems = ctx.Loggings; And you need to add instance of HeatmapViewModel inside the for loop (assuming items is List<HeatmapViewModel>) Commented Nov 1, 2017 at 9:20
  • okay. about first query understood, my fault. But how I need to add instance of HeatmapViewModel in for? @StephenMuecke Commented Nov 1, 2017 at 9:34
  • Inside your if block - items.Add(new HeatmapViewModel{ Latitude2 = filteredQuery[i].Latitude2, ...., Difference = differenceInMinutes}); Commented Nov 1, 2017 at 9:38
  • 1
    You do realize this will only generate a HeatmapViewModel for every 2nd item because of your if block (assuming the Datatype value alternates for each item in the query). Its hard to understand what you really wanting to do here Commented Nov 1, 2017 at 9:44

1 Answer 1

3

Your problem is: allitems is an IEnumerable, so you can't use allitems.Longitude2 to get value of Longitude2. It's not a single item.

I think you should put items.Add(...) block to for loop.

And use filteredQuery[i].Longitude2 instead of allitems.Longitude2.

Like this

       var filteredQuery = (
                            from log in ctx.Loggings
                            where log.Datatype == 1 || log.Datatype == 2
                            orderby log.Id descending
                            select log
                            ).ToList();

        var items = new List<HeatmapViewModel>();

        for (int i = 1; i < filteredQuery.Count; i++)
        {
            if (filteredQuery[i].Datatype == 2 && filteredQuery[i - 1].Datatype == 1)
            {
                TimeSpan differenceTicks = filteredQuery[i].CurDateTime - filteredQuery[i - 1].CurDateTime;

                items.Add(new HeatmapViewModel
                {
                    Latitude2 = filteredQuery[i].Longitude2,
                    Longitude2 = filteredQuery[i].Longitude2,
                    Difference = (int)differenceTicks.TotalMinutes
                });
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.