True, but this does not solve the original problem. The problem is that if
in the select I am accessing Key object several times, eg if I will just do
select new { g.Key.Year, g.Key.ResultId } then I will get duplicated
selects in the sql. And the number of duplicates will be the number of time
I has accessed Key object.
On Monday, 14 January 2013 19:27:07 UTC+2, Gunnar Liljas wrote:
>
> Since you're grouping on the month, it doesn't make sense to select it
> with an aggregate (min).
>
> /g
>
> On 14 jan 2013, at 16:35, Aleksandr Petrov
> <[email protected]<javascript:>>
> wrote:
>
> > Hi,
> >
> > My linq query:
> >
> > from result in results
> > group result by new
> > {
> > Year = result.Timestamp.Year,
> > Month = result.Timestamp.Month,
> > ResultId = result.Id
> > }
> > into g
> > select new { g.Key.Year, g.Key.ResultId, MinMonth = g.Min(y =>
> y.Timestamp.Month) };
> >
> > generated sql:
> >
> > select
> > datepart(year, simplemeas0_.StartTime) as col_0_0_,
> > datepart(month, simplemeas0_.StartTime) as col_1_0_,
> > simplemeas0_.Id as col_2_0_,
> > datepart(year, simplemeas0_.StartTime) as col_3_0_,
> > datepart(month, simplemeas0_.StartTime) as col_4_0_,
> > simplemeas0_.Id as col_5_0_,
> > cast(min(datepart(month, simplemeas0_.StartTime)) as INT) as
> col_6_0_
> > from
> > Measurement simplemeas0_
> > group by
> > datepart(year, simplemeas0_.StartTime) ,
> > datepart(month, simplemeas0_.StartTime) ,
> > simplemeas0_.Id
> >
> > It generates correct sql if in the select is just new { g.Key }, instead
> of new { g.Key.Year, g.Key.ResultId }
> >
> > Is it bug or there is a way to avoid duplication in sql select?
>