C # Silverlight with Entity framework - change return type to AutoGenerated EntityQuery?

Background

I currently have a C # Silverlight business application that uses RIA services. The application is hosted in ASP.NET using the ADO.NET Entity Framework and a domain service class to read and write to a SQL Server database.

Scenario

I have a server-side method in my DomainServiceClass that returns a list of IEnumerable objects. In my ApplicationName.Web.g.cs file I have an autogenerated function too. In my Silverlight Client application, I want to be able to run a foreach loop over the returned list of objects.

DomainServiceClass method:

    public IEnumerable<Image> GetJobImages(string jobGuid)
    {
        var query =
            (
             from j in Context.Job
             orderby (j.ShortCode)
             where j.JobID.Equals(jobGuid)
             join a in Context.Audit //.Distinct()
             on j.JobID equals a.Job.JobID
             join i in Context.Image
             on a.Image.JobID equals i.JobID

             select new Image
             {
                 HighResUrl = i.HighResUrl,
                 LowResUrl = i.LowResUrl,
                 UploadDate = i.UploadDate
                 }).AsEnumerable();

        return query;
    }

      

ApplicationName.Web.g.cs Auto Generation Function:

    /// <summary>
    /// Returns an EntityQuery for query operation 'GetJobImages'.
    /// </summary>
    public EntityQuery<Image> GetJobImagesQuery(string jobGuid)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("jobGuid", jobGuid);
        return base.CreateQuery<Image>("GetJobImages", parameters, false, true);
    }

      

Silverlight client invocation code:

var context = dds.DomainContext as InmZenDomainContext;
                foreach(var item in context.GetJobImagesQuery(currentJob.JobID.ToString())
                {
                   item.etc.....//
                }

      

Problem

Unfortunately, when I try to call this method as above, I get either the error:

'Unable to implicitly convert type System.Windows.Ria.Data.EntityQuery to System.Collections.IEnumerable'.

OR

'Does not contain a public definition for GetEnumerator'.

After reading and chatting with others, I was informed that I need to change the return type to AutoGenerated Function (I think).

Question

Does anyone know how I can change the return type to IEnumerable on this AutoGenerated EntityQuery ?! - Or otherwise, how to solve this problem?

Attempt:

    /// <summary>
    /// Returns an EntityQuery for query operation 'GetJobImages'.
    /// </summary>
    public IEnumerable<Image> GetJobImagesQuery(string jobGuid)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("jobGuid", jobGuid);
        return base.CreateQuery<Image>("GetJobImages", parameters, false, true);
    }

      

(This doesn't work as the method is autogenerated, it just reverts to what it was when I create the solution.)

Help with gratitude.

+2


source to share


2 answers


The problem is that you are missing the step between your call and the IEnumerable collection.

You need to execute EntityQuery and use LoadOperation to get your results. It's not as easy as you think, and here's a good forum post that has an example of how to handle things:

How to get list <> from LoadOperation



Here's the relevant line from the example:

EntityQuery query = Context.GetEmployeesQuery();

Context.Load<Employee>(query).Completed += (sender, args) => {
    List<Employee> list = ((LoadOperation<Employee>)sender).Entities.ToList();
};

      

+5


source


You need to load the query and get the results from the load operation. Your Silverlight client invocation code should be modified as follows:

var context = dds.DomainContext as InmZenDomainContext;
var loadOperation = context.Load(context.GetJobImagesQuery(currentJob.JobID.ToString()));
loadOperation.Completed += (sender, e) => {
  if (!loadOperation.HasError)
    foreach (var item in loadOperation.Entities)
      // Loop over returned items ... Or use LINQ on loadOperation.Entities.
  else
    // loadOperation.Error has details about the error.
    ...
}

      



Note that all "network calls" are asynchronous in Silverlight.

I would also suggest that you change the query parameter from string to GUID.

0


source







All Articles