none
How to count of elements in entity and send it to ViewModel with RIA Services?

    Question

  • Hi, everyone!
    Here is my question. How can I retrieve quantity of Employees?
    In DomainService I have:

            public IQueryable<employee> GetEmployeesCommon()
            {
                return this.ObjectContext. employees
                    .Where(t => t. is_record_delete == false)
                   ;
            }
     
            public int GetNumberOfAllEmployees()
            {
                return GetEmployeesCommon()
                    .Where(t => t.idtypepc >= 2)
                    .Where(t => t.idtypepc <= 11)
                    .Count();
            }

    In DataService:

            public void GetEmployeesAllEmployees(Action<InvokeOperation<int>> getEmployeesCallback)
            {
                Context.GetNumberOfAllEmployees(getEmployeesCallback, null);
            }

    And in ViewModel:

            public void GetTechusAllPCs()
            {
                EmployeeDataService.GetEmployeesAllEmployees(getEmployeesCallback);
            }
    
            private void getEmployeesCallback(InvokeOperation<int> op)
            {
                    EmpAll = op.Value;
            }
    
            private int _empAll;
            public int EmpAll
            {
                get { return _empAll; }
                set
                {
                    _empAll = value;
                    RaisePropertyChanged("EmpAll");
                }
            }

    I call GetTechusAllPCs() in LoadData() (after all entities retrieved) and then EmpAll has right value, but if change count of

    Employees (add or delete one) EmpAll will have same value as before. How can I get actual data?

    Can somebody say what is wrong?





    Thursday, March 03, 2011 7:18 AM

Answers

All replies

  • I am not quite sure what you are expecting, but you need to call GetTecusAllPCs after each add or delete (I assume a delete is to set the deleted flag) to reget the count. Once that is done any control bound to the EmpAll should update.



    Thursday, March 03, 2011 11:14 AM
  • Hi,

    Could you more describe about your scenarios. I did not get.

    Friday, March 04, 2011 5:01 AM
  • Hi,

    Why did you add this property? I am doubt.

    public int EmpAll 
           
    { 
               
    get { return _empAll; } 
               
    set 
               
    { 
                    _empAll
    = value; 
                   
    RaisePropertyChanged("EmpAll"); 
               
    } 
           
    }

    Friday, March 04, 2011 5:03 AM
  • Basically what it looks like you're asking about is a property that is dependant on another.  When one changes, the read-only derivitive needs to also notify subscribers that they should retrieve a new value.  A couple things to do so you can handle this (either way have the int pulled from the employee count in your local collection as opposed to the service call since you'll be manipulating the collection on the client):

    1) ensure that the setter for your collection invokes the RaisePropertyChanged for the property exposing the count

    2) ensure that the collectionchanged event for the collection also invokes the RaisePropertyChanged for the property exposing the count

    Friday, March 04, 2011 7:44 AM
  • Greets,

    If you change anything internally that affects the count of employees (i.e.) from within your ViewModel, where "_empAll" may be changed internally, just make sure you use "RaisePropertyChanged("EmpAll"); " after any change to _empAll.

    Regards,

    Joe

    Saturday, March 05, 2011 12:49 AM
  • Could you more describe about your scenarios. I did not get.

    The hole idea is to make a report, except that count I have to receive several values.

    But now I incline to the idea to get those values directly from Employees within EmployeeViewModel, otherwise those values can be wrong (after i get Employees somebody can change something and send to the server). So the question is how to get that count and some others values (int also) from server?

    Saturday, March 05, 2011 6:42 AM
  • Hi,

    use MergeOption

                System.Data.Objects.ObjectQuery<Customer> customersQuery = this.GetCustomersQuery(_context);
                ObjectResult<Customer> cs = customersQuery.Execute(System.Data.Objects.MergeOption.OverwriteChanges);
                var nc =  cs.Except(mc);
                foreach (var items in nc)
                {
                    mc.Add(items);
                }

    Saturday, March 05, 2011 7:52 AM
  • Hi rojorm,

    Please take a look at below link

    There is a discussion about the data changed notification from server

    http://forums.silverlight.net/forums/p/183265/415583.aspx

     

    Hope this helps

    Thursday, March 10, 2011 6:10 AM
  • http://forums.silverlight.net/forums/p/183265/415583.aspx

    thanks!

    Saturday, March 12, 2011 8:21 AM