How do i access objects of an anonymous type outside the scope where its declared?
for e.g.
void FuncB()
{
var obj = FuncA();
Console.WriteLine(obj.Name);
}
??? FuncA()
{
var a = (from e in DB.Entities
where e.Id == 1
select new {Id = e.Id, Name = e.Name}).FirstOrDefault();
return a;
}
As the other answers have stated, you really shouldn’t do this. But, if you insist, then there’s a nasty hack known as “cast by example” which will allow you to do it. The technique is mentioned in a couple of articles, here and here.
(I can’t take the credit for this hack, although the author of one of those articles says that he doesn’t want to be associated with it either. His name might be familiar.)