2017-10-24 3 views
1

내 프로젝트에 모델이 있습니다. 여기에 모델목록에 항목을 추가하는 방법

나는 데이터베이스에서 일부 항목을 얻을 필요가
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 마지막입니다.

그래서 나는 firstitemlastitem이 목록에 추가 할 필요가 이후 백엔드

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

에이 방법을 쓰기.

나는이 List<Logging> items = new List<Logging> {firstitem, lastItem};

처럼 쓰기하지만

심각도 코드 설명 프로젝트 파일 라인 억제 상태 오류 CS1950 가장 과부하 추가 방법 '에는 list.add (로깅)'에 대한 오류를 얻을 콜렉션 초기화 프로그램에 잘못된 인수가 있습니다. 히트 맵 C : \ Users \ nemes \ source \ repos \ Heatmap \ Heatmap \ Controllers \ HomeController.cs 37 활성 심각도 코드 설명 프로젝트 파일 줄 억제 상태 오류 CS1503 인수 1 : '' 'Heatmap.Models.Logging' '히트 맵 C : \ 사용자 \ nemes 소스 \의 REPOS \의 히트 맵 \의 히트 맵 \ 컨트롤러 \ \ HomeController.cs 37 액티브 내가 목록에 추가 할 수있는 방법이 List<Logging> items = new List<Logging> {firstitem, lastItem};

에 대한

?

답변

3

Logging 대신 익명 형식을 반환합니다. firstitemlastItemAnonymous Types입니다.

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

을 그리고 당신은 여전히 ​​오류가 발생하면 다음이 Logging 기업에서 필요한 특성을 가진 DTO 클래스를 작성해야 매핑 된 엔티티에 투사 할 수 없기 때문에 아마도은 다음과 같습니다 : 여기에 코드를 변경

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

다음 :

x => new LoggingDTO 
+0

당신이 쿼리에서 의미? –

+0

@ S.E 예. 'x => new' 대신'firstitem'과'lastitem'에'x => new Logging'을 사용하십시오. 또한 DTO 클래스 사용에 대한 MY UPDATED 답변을 확인하십시오. –

+0

= x.Longitude2', 심각도 \t 코드 \t 설명 \t 프로젝트 \t 파일 \t 라인 \t 억제 상태 오류 \t CS0117 \t '로깅' '에 대한 정의를 포함하지 않는 나는 그것을 사용하려고하지만 난이'LNG에 대한 오류가 lng '\t Heatmap \t C : \ Users \ nemes \ source \ repos \ Heatmap \ Heatmap \ Controllers \ HomeController.cs 활성 –

관련 문제