I have the following code and it works good:
var _data = (from qu in _que.GetAll(
u => u.company == "GE" )
select new {
qu.name,
qu.address
});
The signature of the GetALL method is
ICollection<T> GetAll(Expression<Func<T, bool>> predicate);
Now I need to enclose the above in some block so it becomes:
{
var _data = (from qu in _que.GetAll(
u => u.company == "GE" )
select new {
qu.name,
qu.address
});
}
var _abc = _data; <<< doesn't work now
Once I do this then _data becomes local and I can’t access it from outside the block. I assume that what I need to do is to declare _data outside of the block. But what do I declare it too as they type that’s returned and placed into _data is an anonymous type. Is there some way I can declare _data without having to change the query or make up some return type?
Your question is unclear, but I think I understand it, just about. Personally I try to avoid requiring this, but you can do something like:
So long as you use the same property names and types, the two anonymous types here will be equivalent, so you can perform the second assignment with no problems. Note that I’ve changed the formatting of your query expression – I believe this looks much clearer, as previously it looked like the
GetAllargument was part of the query expression itself.Given that your query expression is just a single select, I’d probably actually just write: