How can I declare a variable outside of a block when the variable defines an anonymous class in C# -


i have following code , works good:

var _data = (from qu in _que.getall(   u => u.company == "ge" )   select new {     qu.name,     qu.address }); 

the signature of getall method

icollection<t> getall(expression<func<t, bool>> predicate); 

now need enclose above in block becomes:

{     var _data = (from qu in _que.getall(       u => u.company == "ge" )       select new {         qu.name,         qu.address     }); } var _abc = _data; <<< doesn't work 

once _data becomes local , can't access outside block. assume need declare _data outside of block. declare type that's returned , placed _data anonymous type. there way can declare _data without having change query or make return type?

your question unclear, think understand it, about. try avoid requiring this, can like:

// i'm assuming name , address strings var _data = enumerable.repeat(new { name = "", address = "" }, 0); _data = null; // never want use value  if (somecondition) {     _data = qu in _que.getall(u => u.company == "ge")             select new { qu.name, qu.address }; } 

so long use same property names , types, 2 anonymous types here equivalent, can perform second assignment no problems. note i've changed formatting of query expression - believe looks much clearer, looked getall argument part of query expression itself.

given query expression single select, i'd write:

_data = _que.getall(u => u.company == "ge")             .select(qu => new { qu.name, qu.address }); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -