如何将项目添加到列表 [英] How adding items to list

查看:181
本文介绍了如何将项目添加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有模型.这是模型代码

I have model in my project. Here is code of model

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 string Speed { get; set; }
    public Nullable<int> Datatype { get; set; }
    public int Id { get; set; }
    [NotMapped]
    public TimeSpan? FirstStartDifference
    {
        get
        {
            if (CurDateTime != null)
            {
                var midnight = new DateTime(CurDateTime.Year, CurDateTime.Month, CurDateTime.Day, 00, 00, 00);
                var difference = CurDateTime - midnight;
                return difference;
            }
            return null;
        }
    }
    [NotMapped]
    public TimeSpan? LastStartDifference
    {
        get
        {
            if (CurDateTime != null)
            {
                var midnight = new DateTime(CurDateTime.Year, CurDateTime.Month, CurDateTime.Day, 23, 59, 00);
                var difference = midnight - CurDateTime;
                return difference;
            }
            return null;
        }
    }
    [NotMapped]
    public int coeff = 2;
}

我需要从数据库中获取一些项目,这是第一个条目,其中Datatype==1,最后一个是Datatype ==2.

I need to get some items from database , it's first entry, where Datatype==1 and Last where Datatype ==2.

所以我在后端写了这个方法

So I write this method on back-end

public JsonResult GetStops()
{
    using (var ctx = new GoogleMapTutorialEntities())
    {      
        var firstitem = ctx.Loggings.Where(x => x.Datatype == 2).AsEnumerable().Select(
               x => new
               {
                   lng = x.Longitude2,
                   lat = x.Latitude2,
                   difference = (int)(x.FirstStartDifference?.TotalMinutes ?? -1) * x.coeff
               }).FirstOrDefault();
        var lastItem = ctx.Loggings.Where(x => x.Datatype == 2).AsEnumerable().Select(
               x => new
               {
                   lng = x.Longitude2,
                   lat = x.Latitude2,
                   difference = (int)(x.LastStartDifference?.TotalMinutes ?? -1) * x.coeff
               }).LastOrDefault();
        List<Logging> items = new List<Logging> {firstitem, lastItem};
        return Json(firstitem, JsonRequestBehavior.AllowGet);
    }
}

此后,我需要将firstitemlastitem添加到列表中.

After this I need to add firstitem and lastitem to list.

我这样写List<Logging> items = new List<Logging> {firstitem, lastItem};

但是我得到一个错误

严重性代码描述项目文件行抑制状态 错误CS1950集合初始化程序的最佳重载添加方法'List.Add(Logging)'有一些无效的参数Heatmap C:\ Users \ nemes \ source \ repos \ Heatmap \ Heatmap \ Controllers \ HomeController.cs 37活动 严重性代码说明项目文件行抑制状态 错误CS1503参数1:无法从''转换为'Heatmap.Models.Logging'Heatmap C:\ Users \ nemes \ source \ repos \ Heatmap \ Heatmap \ Controllers \ HomeController.cs 37有效

Severity Code Description Project File Line Suppression State Error CS1950 The best overloaded Add method 'List.Add(Logging)' for the collection initializer has some invalid arguments Heatmap C:\Users\nemes\source\repos\Heatmap\Heatmap\Controllers\HomeController.cs 37 Active Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from '' to 'Heatmap.Models.Logging' Heatmap C:\Users\nemes\source\repos\Heatmap\Heatmap\Controllers\HomeController.cs 37 Active

为此List<Logging> items = new List<Logging> {firstitem, lastItem};

如何将它们添加到列表中?

How I can add them to List?

推荐答案

您将返回匿名类型,而不是Logging. firstitemlastItem

You are returning an anonymous type instead of Logging. The firstitem and lastItem are Anonymous Types. Change your code to this:

x => new Logging
{
    Longitude2 = x.Longitude2,
    Latitude2 = x.Latitude2,
    //And other properties
}

如果仍然出现错误,可能是因为无法投影到映射的实体,则需要创建

And if you still get error probably it is because you cannot project onto a mapped entity then you need to create a DTO class with needed properties from the Logging entity:

public class LoggingDTO
{
    public string Longitude2 { get; set; }
    public string Latitude2  { get; set; }
    //And other properties
}

然后:

x => new LoggingDTO

这篇关于如何将项目添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆