i am using LINQ to entity to return a list of objects
var st = personsList.Select(p => new
{
ID = p.Id,
Key = p.Key,
Name = p.Name,
Address = p.Address,
City = p.City,
PhoneNumber = p.PhoneNumber
})
return st.ToList();
after i get the list in another class how can I access each property?
something like
foreach(object s in St)
{
string name = s.Name;
}
I have no predefined class to cast the object to it.
Can this be done without having to create a class and cast the object to that class type?
Thanks
Are you using C# 4? You could try using dynamic
:
foreach(dynamic s in St)
{
string name = s.Name;
}
The risk is, of course, that if you try to access a property that the object doesn't have you'll only find out at runtime.
As an aside, wouldn't it make more sense in this case to actually create a class that has all these properties? You clearly need them in several places and you'd get the benefits of type-safety and compile-time errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With