none
lamda statement on observableCollection return null RRS feed

  • Question

  • I am trying to fetch data using below LINQ lamda query but unable to get some result . it always return null. Why?

     

      this.dgStructRental.ItemsSource = GetCurrentRentals();           
              
            }
    
            private ObservableCollection<PROPOSALRENTALDETAILInfo> GetCurrentRentals()
            {
                return (StructuredRentalViewModel.ProposalRentalDetails
                                                .Where(w => w.RowState.Equals(proposalProxy.DataRowState.Added))
                                                .Select(s => s)) as ObservableCollection<PROPOSALRENTALDETAILInfo>;
            }
    

    the Get current Rentals method always return null.

    I have tried below query too but still unable to  fetch some results.

    private ObservableCollection<PROPOSALRENTALDETAILInfo> GetCurrentRentals()
            {
                var query = (from sr in StructuredRentalViewModel.ProposalRentalDetails
                            where sr.RowState.ToString().Equals(proposalProxy.DataRowState.Added.ToString())
                            select sr) as ObservableCollection<PROPOSALRENTALDETAILInfo>;
    
                return query;
            }

     


    maifs
    Thursday, December 8, 2011 6:53 AM

Answers

  • Hi Mehmood;

    I believe that the issue may be that the query is returning a collection of IEnumerable<ProposalRentalDetails> or IQueryable<ProposalRentalDetails> and these types can not be cast to a collection of ObservableCollection<ProposalRentalDetails>.

    From Microsoft documentation, "The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form: expression as type is equivalent to: expression is type ? (type)expression : (type)null", and most likely the reason why you are getting null all the time.

    To convert the query to a ObservableCollection you can do the following:

    var results = StructuredRentalViewModel.ProposalRentalDetails
                    .Where(w => w.RowState.Equals(proposalProxy.DataRowState.Added))
                    .Select(s => s);
                   
    var ocResults = new ObservableCollection(results);


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Mehmood Ahmed Thursday, December 8, 2011 7:46 PM
    Thursday, December 8, 2011 3:35 PM
  • The cast to ObservableCollection will not work, as the query isn't going to return that.

     

    You must construct a new collection from your results, like so:

     

            private ObservableCollection<PROPOSALRENTALDETAILInfo> GetCurrentRentals()
            {
                return new ObservableCollection<PROPOSALRENTALDETAILInfo>(StructuredRentalViewModel.ProposalRentalDetails
                                                .Where(w => w.RowState.Equals(proposalProxy.DataRowState.Added)));
            }
    

    This will run your query, and use its results to populate a new OC<T>.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 8, 2011 7:02 AM

All replies

  • The cast to ObservableCollection will not work, as the query isn't going to return that.

     

    You must construct a new collection from your results, like so:

     

            private ObservableCollection<PROPOSALRENTALDETAILInfo> GetCurrentRentals()
            {
                return new ObservableCollection<PROPOSALRENTALDETAILInfo>(StructuredRentalViewModel.ProposalRentalDetails
                                                .Where(w => w.RowState.Equals(proposalProxy.DataRowState.Added)));
            }
    

    This will run your query, and use its results to populate a new OC<T>.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 8, 2011 7:02 AM
  • Hi Mehmood;

    I believe that the issue may be that the query is returning a collection of IEnumerable<ProposalRentalDetails> or IQueryable<ProposalRentalDetails> and these types can not be cast to a collection of ObservableCollection<ProposalRentalDetails>.

    From Microsoft documentation, "The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form: expression as type is equivalent to: expression is type ? (type)expression : (type)null", and most likely the reason why you are getting null all the time.

    To convert the query to a ObservableCollection you can do the following:

    var results = StructuredRentalViewModel.ProposalRentalDetails
                    .Where(w => w.RowState.Equals(proposalProxy.DataRowState.Added))
                    .Select(s => s);
                   
    var ocResults = new ObservableCollection(results);


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Mehmood Ahmed Thursday, December 8, 2011 7:46 PM
    Thursday, December 8, 2011 3:35 PM
  • Hi Mehmood,

    I'm glad to hear that you have sovled the issue, if you have any problem, please feel free to post in the forum.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 9, 2011 2:09 AM
    Moderator