ItemsSource set to LINQ Query Expression

5 posts, 0 answers
  1. Doug Jones
    Doug Jones avatar
    4 posts
    Member since:
    Apr 2010

    Posted 27 Apr 2010 Link to this post

    I'm using Silverlight 3 Q1 2010 version of the controls and specifically trying to set the ItemsSource for a RadChart.  I can successfully set my list using a lambda expression or a LINQ query expression:

    RadChart1.ItemsSource = Assignments.Where(x => x.StatusCode != "Status1"); 




    OR
    RadChart1.ItemsSource = (from x in Assignments 
                                         where x.StatusCode != "Status1" 
                                         select x); 

    If I change the output of the LINQ query expression, however:

    RadChart1.ItemsSource = (from x in Assignments 
                                        where x.StatusCode != "Status1" 
                                         select new NewStatus 
                { 
                    StatusCode = x.StatusCode, 
                    StatusCount = x.StatusCount 
                }); 

     the code blows up with a NullReferenceException.  Has anyone come across this issue before?  Querying against it works as long as the output isn't changed.  If the output's changed in any way (in this case different class with the same properties), it throws an error.


  2. Ves
    Admin
    Ves avatar
    2926 posts

    Posted 30 Apr 2010 Link to this post

    Hello Doug,

    Please, find attached a small example, which follows your description and works correctly for me. Give it a try and let me know if I have missed something.


    All the best,
    Ves
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Doug Jones
    Doug Jones avatar
    4 posts
    Member since:
    Apr 2010

    Posted 30 Apr 2010 Link to this post

    Thank you very much for looking into this and creating the sample.  I looked it over and was at least able to narrow down the problem.

    My Assignments list in my example is of type List<DataAccess.Status> and I am trying to convert to a different class, NewStatus.

    That's where the issue lies.  After running your example, which ran successfully, I ran mine again with the select of the linq query selecting a new DataAccess.Status and it worked perfectly.  I originally noticed this problem, however, when running a groupby, which forces me to change that class type.

    Could you please try creating another class with the same properties and selecting new of that class in the linq query?  You should get a nullreferenceexception.

     

  4. Ves
    Admin
    Ves avatar
    2926 posts

    Posted 05 May 2010 Link to this post

    Hello Doug,

    I am afraid I am still unable to reproduce it. I have attached an updated version as per your description. Is there anything else that might make the difference?

    Greetings,
    Ves
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Doug Jones
    Doug Jones avatar
    4 posts
    Member since:
    Apr 2010

    Posted 05 May 2010 Link to this post

    Ves, thank you for all your help!

    I ran your example successfully and then noticed that you had the "get; set;" for each property in the class.  I had not added that in my class.  I found that removing it from your My1 class properties caused the nullreferenceexception.  When I added it to my properties, everything worked successfully!

    I looked up the reason for this and found an example here
    http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html

    What I was using was a public variable instead of a property and it needed to be a property since you can't databind against a variable.


    Thanks again
Back to Top