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;
}
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);
}
}
After this I need to add firstitem and lastitem to list.
I write it like this List<Logging> items = new List<Logging> {firstitem, lastItem};
But I get an error
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:UsersnemessourcereposHeatmapHeatmapControllersHomeController.cs 37 Active
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from ” to ‘Heatmap.Models.Logging’ Heatmap C:UsersnemessourcereposHeatmapHeatmapControllersHomeController.cs 37 Active
for this List<Logging> items = new List<Logging> {firstitem, lastItem};
How I can add them to List?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
}
Then:
x => new LoggingDTO
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0