Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities Group By expression gives 'Anonymous type projection initializer should be simple name or member access expression'

I am getting the above mentioned error with this expression:

var aggregate = from t in entities.TraceLines
    join m in entities.MethodNames.Where("it.Name LIKE @searchTerm", new ObjectParameter("searchTerm", searchTerm)) on t.MethodHash equals m.MethodHash
    where (t.CallTypeId & (int)types) == t.CallTypeId && t.UserSessionProcessId == m_SessionId
    group t by m.Name into d                                                   
    select new
    {
        d.Key,                                     
        d.Sum(x => x.InclusiveDurationMilliseconds) // <- squigglies on this line
    };

Any idea what is causing this error?

like image 227
esac Avatar asked Jun 22 '10 21:06

esac


People also ask

What is anonymous type in LINQ?

Anonymous types typically are used in the select clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see LINQ in C#. Anonymous types contain one or more public read-only properties.

Can you project a query to an anonymous type?

In some cases, you might want to project a query to a new type, but the query would be your only use for the new type. Rather than create the type, you can project to an anonymous type.

What is the difference between an anonymous type and a regular data type?

The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.


1 Answers

Do something like:

select new
{
    d.Key,
    Sum = d.Sum(x => x.InclusiveDurationMilliseconds)
};

It can project a property name from another property, but not from a method....

like image 77
Matthew Abbott Avatar answered Oct 11 '22 16:10

Matthew Abbott