Change Return Type On AutoGenerated EntityQuery in C# Silverlight with Entity Framework?

c# entity-framework ienumerable silverlight

Question

Background

Currently, I am using RIA Services in a C# Silverlight business application. The ADO.NET Entity Framework and a domain service class are used to read and write to the SQL Server database while the application is hosted in ASP.NET.

Scenario

My DomainServiceClass has a server-side function that returns an IEnumerable list of objects. Additionally, I have an autogenerated method in my ApplicationName.Web.g.cs file. I want to be able to go over the returned list of items using a foreach loop in my Silverlight client application.

Method DomainServiceClass:

    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;
    }

AutoGenerated Function for ApplicationName.Web.g.cs

    /// <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 attempt to use this approach as described above, I either get the following error:

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

OR

'Does not contain a public definition for GetEnumerator'.

I've learned through reading and speaking with others that I must modify the return type for the AutoGenerated Function (I think).

Question

Can somebody tell me how to get this AutoGenerated EntityQuery's result type to be IEnumerable? - Or of an other approach to resolving this issue?

Attempts were made to:

    /// <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 fails because the method is autogenerated; when I build the solution, it just reverts to the previous state.)

Much grateful assistance.

1
2
9/28/2009 12:31:39 PM

Accepted Answer

You're missing a step between your call and the IEnumerable collection, which is the source of the issue.

To get your results, you must run the EntityQuery and use LoadOperation. It's not as simple as you would assume, and this forum post provides a useful illustration of how to proceed:

What to do to get a List from a LoadOperation

The relevant portion of the example is as follows:

EntityQuery query = Context.GetEmployeesQuery();

Context.Load<Employee>(query).Completed += (sender, args) => {
    List<Employee> list = ((LoadOperation<Employee>)sender).Entities.ToList();
};
5
9/28/2009 1:07:14 PM

Popular Answer

You must load the query in order to get the load operation's results. You should change your Silverlight client invocation code to look something like this:

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.
    ...
}

Keep in mind that with Silverlight, all "network calls" are asynchronous.

I'd also advise you to think about switching the query parameter from a string to a GUID.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow