Solo los constructores e inicializadores sin parámetros son compatibles con LINQ to Entities

There are several same questions that have same problem. But I cant find solution for me.

LinQ

var result = (from a in entity.TblAnalizorReadings
             group a by new { date = new DateTime(((DateTime)a.okuma_tarihi).Year, ((DateTime)a.okuma_tarihi).Month, ((DateTime)a.okuma_tarihi).Day, ((DateTime)a.okuma_tarihi).Hour, 0, 0) } into g
             select new AnalizorPivotChartModel
             {
                  okuma_tarihi = g.Key.date,
                  Gerilim_Faz1 = g.Max(x => x.Gerilim_Faz1),
                  Gerilim_Faz2 = g.Max(x => x.Gerilim_Faz2),
                  Gerilim_Faz3 = g.Max(x => x.Gerilim_Faz3)
              }).ToList();

Modelo

public class AnalizorPivotChartModel
{
    public Nullable<System.DateTime> okuma_tarihi { get; set; }
    public Nullable<decimal> Gerilim_Faz1 { get; set; }
    public Nullable<decimal> Gerilim_Faz2 { get; set; }
    public Nullable<decimal> Gerilim_Faz3 { get; set; }
}

I get error message as this question title. I can write more code if its neccesary.

Gracias.

preguntado el 28 de agosto de 12 a las 08:08

No There is no default constructor. But I can use another class that has no constructor, too. -

Can this issue be connected with DateTime constructor? -

I solved it. I get readings first, like this: IEnumerable readings=entity.TblAnalizorReadings and write above code(I get datas from readings). Is this good approach? OR any other way? -

@AliRızaAdıyahşi you won't get database-side grouping this way. If performance is your concern, you should find other ways (e.g. don't create DateTime for grouping, you don't need it anyway) -

@SergRogovtsev Why is this a bad target - to get a server-side grouping by date part of the datetime column, returning some aggregate values of the resulting groups? If EF is not ready to provide such SQL, is a stored procedure a better approach? -

1 Respuestas

var result = entity.TblAnalizorReadings
    .GroupBy(a =>
        new {
            ((DateTime)a.okuma_tarihi).Year,
            ((DateTime)a.okuma_tarihi).Month,
            ((DateTime)a.okuma_tarihi).Day,
            ((DateTime)a.okuma_tarihi).Hour
        },
        (k, g) => new {
            okuma_tarihi = k,
            Gerilim_Faz1 = g.Max(x => x.Gerilim_Faz1),
            Gerilim_Faz2 = g.Max(x => x.Gerilim_Faz2),
            Gerilim_Faz3 = g.Max(x => x.Gerilim_Faz3)
        })
    .AsEnumerable()
    .Select(g => new AnalizorPivotChartModel
         {
              okuma_tarihi = new DateTime(okuma_tarihi.Year, okuma_tarihi.Month, okuma_tarihi.Day, okuma_tarihi.Hour, 0, 0),
              Gerilim_Faz1 = g.Gerilim_Faz1,
              Gerilim_Faz2 = g.Gerilim_Faz2,
              Gerilim_Faz3 = g.Gerilim_Faz3
          })
    .ToList();

Respondido 28 ago 12, 11:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.