如何将项目添加到列表

如何将项目添加到列表

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

问题描述

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

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

但是我得到一个错误

为此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
}

如果仍然出现错误,可能是因为无法投影到映射的实体,则需要创建具有Logging实体所需属性的DTO 类:

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

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

07-25 05:46