How can I write this in one query?
var query = this.ObjectContext.SomeCollection.
.Where(...)
.Select(f => new {somenumber = f.somenumber});
MyType type = new MyType()
{
Sum = query.Sum(f => f.somenumber)
}
Your use of the anonymous type is completely unnecessary since you have only one property in the projection. You can simply take the query and enclose it inside the object initializer for
MyType. Note that this is fine as long as you’re not reusing the projection elsewhere (in which case you would pull it outside and then reuse it).Additionally, you could reduce
.Select(f => f.somenumber).Sum()toSum(f => f.somenumber).