This is a migrated thread and some comments may be shown as answers.

ItemsSource set to LINQ Query Expression

4 Answers 111 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Doug Jones
Top achievements
Rank 1
Doug Jones asked on 27 Apr 2010, 07:12 PM
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.


4 Answers, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 30 Apr 2010, 12:20 PM
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.
0
Doug Jones
Top achievements
Rank 1
answered on 30 Apr 2010, 01:39 PM

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.

 

0
Ves
Telerik team
answered on 05 May 2010, 09:27 AM
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.
0
Doug Jones
Top achievements
Rank 1
answered on 05 May 2010, 01:23 PM
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
Tags
General Discussions
Asked by
Doug Jones
Top achievements
Rank 1
Answers by
Ves
Telerik team
Doug Jones
Top achievements
Rank 1
Share this question
or