New Posts New Posts RSS Feed: problem in Return Asyncronous Method
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

problem in Return Asyncronous Method

 Post Reply Post Reply
Author
bala View Drop Down
Groupie
Groupie
Avatar

Joined: 18-Aug-2010
Location: Brazil
Posts: 54
Post Options Post Options   Quote bala Quote  Post ReplyReply Direct Link To This Post Topic: problem in Return Asyncronous Method
    Posted: 09-Sep-2010 at 10:18am
HI folks

I have to populate the  List for that I write following code

 public class Teste
    {

         private List<PESSOA_CARRO_MARCA> _lista;

        public List<PESSOA_CARRO_MARCA> RetornaTeste()
        {
            var mgr = new EntitiesTeste();
            var query = mgr.PESSOA_CARRO_MARCA.OrderBy(f=>f.DESCRICAO);
            query.ExecuteAsync(op => 
            {
                if (op.IsCompleted)
                {
                    _lista = new List<PESSOA_CARRO_MARCA>();
                    foreach (var item in query)
                    {
                        _lista.Add(new PESSOA_CARRO_MARCA() { DESCRICAO= item.DESCRICAO,  ATIVO= item.ATIVO});            
                    }
                    return _lista;   >>>> Here is error      
                }
            });    
        }

But Ideablade gives me error
(1) System.Action<IdeaBlade.EntityModel.EntityQueryOperation> returns  void,a return keyword
must not be followed by an object expression.

(2)Cannot convert lambda expression to delegate type 'System.Action<IdeaBlade.EntityModel.EntityQueryOperation>'

because some of the return types in the block are not implicitly convertible to the delegate return type)


If we put return _lista at end of mehtod its compiling but throwing run time exception .

Object reference not set to an instance of an object.

Thanks

How can I solve this problem

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 10-Sep-2010 at 2:43pm
Bala;

Put your _lista initialization and return statement outside your lambda expression so your code should look something like this:

 public class Teste
    {

         private List<PESSOA_CARRO_MARCA> _lista;

        public List<PESSOA_CARRO_MARCA> RetornaTeste()
        {
            var mgr = new EntitiesTeste();
            var query = mgr.PESSOA_CARRO_MARCA.OrderBy(f=>f.DESCRICAO);                             

            _lista = new List<PESSOA_CARRO_MARCA>();

            query.ExecuteAsync(op => 
            {
                if (op.IsCompleted)
                {
                    foreach (var item in query)
                    {
                        _lista.Add(new PESSOA_CARRO_MARCA() { DESCRICAO= item.DESCRICAO,  ATIVO= item.ATIVO});            
                    }
                }
            });

           return _lista;
        }

Back to Top
bala View Drop Down
Groupie
Groupie
Avatar

Joined: 18-Aug-2010
Location: Brazil
Posts: 54
Post Options Post Options   Quote bala Quote  Post ReplyReply Direct Link To This Post Posted: 13-Sep-2010 at 4:24am
Hi Densk
But the methoda return Asyncrnous  then list return (null) before loading
having error.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 13-Sep-2010 at 3:14pm
Try using an ObservableCollection instead and declare the variable public. With asynchronous calls, you have to implement things differently and returning a list like what you intend to do is not straightforward.
Back to Top
bala View Drop Down
Groupie
Groupie
Avatar

Joined: 18-Aug-2010
Location: Brazil
Posts: 54
Post Options Post Options   Quote bala Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2010 at 6:30am
Hi

Thanks very much for solution
but I adopted other way and solve the problem

Thanks again
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down