views:

38

answers:

1

I just started learning Silverlight by walking through the labs posted on Channel9. When I tried to explore a little bit I found that my queries were not working as I thought they would.

To recreate what I have done you would need to create a new Silverlight Business application, create a data entity that is pointed to the Adventureworks LT db, and generate the web services for those entities (including edit).

I then simply drug a RichTextbox to Home.xaml and in Home.xaml.cs I added this code first to OnNavigatedTo and when that didn't work to the constructor.

    AdventureWorksDomainContext ctx = new AdventureWorksDomainContext();

    EntityQuery<Product> query =
        from p in ctx.GetProductsQuery()
        select p;
    LoadOperation<Product> loadOp = ctx.Load(query);

    var paragraph = new Paragraph();
    foreach (var product in loadOp.Entities)
    {
        paragraph.Inlines.Add(new Run { Text = product.Name });
    }

    richTextBox1.Blocks.Add(paragraph);

When I run the page I never see loadOp.Entities contain a value and I only see the query I expect, go across the wire after all my code has been executed.

I feel like I'm missing something fundamental and this will make more sense if I can find someone to explain it to me.

Thanks, Eric

A: 

The problem is related to the how you are loading the data. The actual Load operation is asynchronous, as is all Silverlight network calls. You are callingt ctx.Load(query) and then immediately setting the paragraph to the entities. You need to use a callback when Load is completed. Something like this,

    AdventureWorksDomainContext ctx = new AdventureWorksDomainContext();

EntityQuery<Product> query =
    from p in ctx.GetProductsQuery()
    select p;
LoadOperation<Product> loadOp = ctx.Load(query,() => 
{
    var paragraph = new Paragraph();
    foreach (var product in loadOp.Entities)
    {
        paragraph.Inlines.Add(new Run { Text = product.Name });
    }

    richTextBox1.Blocks.Add(paragraph);
});

Since you aren't using the entities directly in a binding and are just iterating them, you need to make sure you wait until they are loaded. I can't remember the actual signature of the Load method, so you may need to modify my lambda to make it work.

Stephan
What you are saying makes sense and that I need to treat it like an AJAX call. I will try when I get home tonight.
Eric Neunaber