On Mon, 06 Oct 2008 10:06:22 -0700, shapper <md*****@gmail.comwrote:
[...]
Basically, what I would like to do, but not knowing if possible is:
- Create the items in a generic way (Not creating a class for this):
new { Name = Role.Administrator, Description = "Administrador", open
= false }
I don't understand why you have a parameter "open" that's never used.
That said, you can only get part of what you're looking for using syntax
similar to what you've posted (I've used "..." rather than writing out all
the code):
return new object[] { new { Name = .... }, ... };
Using some contortions, you could get a "var"-declared collection instead
on which you could successfully call OrderBy() with the lambda expression
you posted. But it starts to get really messy at that point, and even
once you've done that, the anonymous type won't be visible outside your
GetRoles() method.
- Being able to use Linq on the result:
GetRoles(new CultureInfo("en-GB" , true)).Where(r =r.open = false)
As long as you return an IEnumerable, you can use LINQ. The problem is
that the compiler won't have information about the type by the time the
collection instance gets back from the GetRoles() method. You'd have to
use reflection to inspect the type, get the "open" property, and then use
the associated PropertyInfo to get the value inside your LINQ expression.
Reflection is slow enough as it is, but you'd have to do it for each
element of the collection, which could really bog things down.
It's not clear at all from your question _why_ you want to do this
though. Even if this could have worked cleanly, it would have required
that the type be consistent throughout, which seems to me to be a great
justification for just going ahead and declaring the relevant type. Why
do you have such an aversion to declaring the type?
Pete