S.E
S.E

Reputation: 107

Assign value to new List property (ASP.NET MVC)

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?

Upvotes: 0

Views: 1637

Answers (1)

TriV
TriV

Reputation: 5148

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
                });
            }
        }

Upvotes: 3

Related Questions