Print Page | Close Window

problem in Return Asyncronous Method

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2137
Printed Date: 10-Jun-2026 at 7:09pm


Topic: problem in Return Asyncronous Method
Posted By: bala
Subject: problem in Return Asyncronous Method
Date 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




Replies:
Posted By: DenisK
Date 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;
        }



Posted By: bala
Date Posted: 13-Sep-2010 at 4:24am
Hi Densk
But the methoda return Asyncrnous  then list return (null) before loading
having error.


Posted By: DenisK
Date 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.


Posted By: bala
Date Posted: 14-Sep-2010 at 6:30am
Hi

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

Thanks again



Print Page | Close Window