Print Page | Close Window

From Class to Linq

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1996
Printed Date: 28-Mar-2025 at 9:35am


Topic: From Class to Linq
Posted By: BillG
Subject: From Class to Linq
Date Posted: 22-Jul-2010 at 4:43pm
I used to write a retrieval query like this
EntityList<Customer> customers = myPM.GetEntities<Customer>(query);
 
 
And then I would pass the customers object to a data grid.
 
How do I do the same in Linq
 
This is what I have so far:
 

private readonly TicketWareEntities entityManager = TicketWareEntities.DefaultManager;

public ObservableCollection<Vendor> Vendors { get; private set; }

public ????? FetchVendors()

{

var query = from Vendor in entityManager.Vendors

orderby Vendor.VendorName

select Vendor;

????? = entityManager.ExecuteQuery<Vendor>(query);

return ?????

}



Replies:
Posted By: GregD
Date Posted: 23-Jul-2010 at 11:23am
Queries in Silverlight (which I assume you're using since you posted in this forum) must be asynchronous unless directed against the local cache. So the basic query would look something like this:


      var vendorsQuery = entityManager.Vendors
        .OrderBy(v => v.VendorName);
      _em1.ExecuteQueryAsync<Vendor>(vendorsQuery, GotVendors, null);
...
    private void GotVendors(EntityQueryOperation<Vendor> op) {
      ObservableCollection<Vendor> vendors =
        new ObservableCollection<Vendor>();
      op.Results.ForEach(v => vendors.Add(v));
    }


or using the anonymous method syntax,


      var vendorsQuery = entityManager.Vendors
        .OrderBy(v => v.VendorName);
      entityManager.ExecuteQueryAsync<Vendor>(vendorsQuery, op => {
        ObservableCollection<Vendor> vendors =
          new ObservableCollection<Vendor>();
        op.Results.ForEach(v => vendors.Add(v));
      },
      null);
    }


If you want write a FetchVendors method, you'll need to pass it a callback method so it can "phone" your calling method back when the results have been returned from the asynchronous query operation. For an example of that, see the GetEmployeeWithTotalOrderRevenueAsyncMethod() mini-demo in the Silvermight MiniDemos solution for Object Persistence, described here:

http://www.ideablade.com:8989/xwiki/bin/view/Documentation/Code_MiniDemosSilverlight_BusObjPersistence - DevForce 2010 Documentation » Business Object Persistence » Code Sample - Mini-Demos (Silverlight)



Print Page | Close Window