为新的List属性赋值(ASP.NET MVC)

时间:2017-11-01 09:12:12

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我有表类Logging

这是代码:

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

}

我也有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;
}

我在存储库中有方法可以进行所有计算

这是代码

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

我遇到了这段代码的问题:

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

这是错误:

  

严重级代码描述项目文件行抑制状态   错误CS1061'IEnumerable'不包含'Longitude2'的定义,并且没有扩展方法'Longitude2'接受类型'IEnumerable'的第一个参数可以找到(你是否缺少using指令或汇编引用?)Heatmap C:\ Users \ nemes \ source \ repos \ Heatmap \ Heatmap \ Repository \ HeatmapRepository.cs 91 Active

     

严重级代码描述项目文件行抑制状态   错误CS1061'IEnumerable'不包含'Longitude2'的定义,并且没有扩展方法'Longitude2'接受类型'IEnumerable'的第一个参数可以找到(你是否缺少using指令或汇编引用?)Heatmap C:\ Users \ nemes \ source \ repos \ Heatmap \ Heatmap \ Repository \ HeatmapRepository.cs 92 Active   严重性代码描述项目文件行抑制状态   错误CS0103当前上下文中不存在名称“differenceInMinutes”Heatmap C:\ Users \ nemes \ source \ repos \ Heatmap \ Heatmap \ Repository \ HeatmapRepository.cs 93 Active

我如何解决它们?

1 个答案:

答案 0 :(得分:3)

您的问题是:allitemsIEnumerable,因此您无法使用allitems.Longitude2获取Longitude2的值。它不是单一项目。

我认为你应该items.Add(...)阻止for循环。

并使用filteredQuery[i].Longitude2代替allitems.Longitude2

喜欢这个

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