Unfortunately, this is not possible, even with clearly typed objects. This is due to the way object initializers work. For instance:
public class MyClass { public int Age = 10; public bool IsLegal = Age > 18; }
Sets this compiler error to "IsLegal":
Error 1 The field initializer cannot refer to non-static fields, method or property "MyClass.Age" ...
A field initializer cannot reference other non-static fields, and since anonymous types do not create static fields, you cannot use the value of one field to initialize another. The only way around this is to declare variables outside of the anonymous type and use them inside the initializer.
int age = GetAgeFromSomewhere(id); var profile = new { Age = age, IsLegal = age > 18 };
source share