New Posts New Posts RSS Feed: Typed result set possible?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Typed result set possible?

 Post Reply Post Reply
Author
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Topic: Typed result set possible?
    Posted: 26-Feb-2010 at 4:39pm
Is it possible to define a typed result set for use in an asynchronous query?  A simplified example:

  public class EmpName { public string FirstName, LastName; }
...
   DomainModelEntityManager mgr = DomainModelEntityManager.DefaultManager;
   var query = mgr.Employees.Select(e => new
EmpName { FirstName = e.FirstName, LastName = e.LastName });
   mgr.ExecuteQueryAsync(query, callback, null);
...
    public void callback(EntityFetchedEventArgs<
EmpName> args)
    {
        // do something useful with the IEnumerable<EmpName>
    }


I've been able to make it work with anonymous types, but it just seems so inelegant.  Running the above I get "Object reference not set to an instance of an object." as soon as it tries to build the query.

Thanks!
Back to Top
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Posted: 01-Mar-2010 at 4:20pm

What you have above will work if you make FirstName and LastName properties of EmpName rather than fields.

To make it work in Silverlight, you must also do the following:

  1. Put EmpName in a class that is accessible both server- and client-side (e.g., put in in the web project, then add it as a link to the SharedCode folder in the Silverlight project).
  2. Either have your EmpName class implement IKnownType, or mark it with the [DiscoverableType(DiscoverableTypeMode.KnownType)] attribute.  Doing either will enable DevForce to inform the serializer up front about your type so that it will serialize it properly when it encounters it.
  3. We also recommend that you attribute the class (EmpName) with the [DataContract] attribute, and each of its members with the [DataMember] attribute. Marking the type with the [DataContract] attribute means that only members marked with the [DataMember] attribute will be serialized; otherwise, the serializer will try to serialize all public members. There may be also other circumstances in which explicitly marking the type and its members with the DCS attributes will prevent trouble.

So here, for example, is what your EmpName class should look like:

[DataContract]
  public class EmpName:IKnownType {
    //public string FirstName, LastName;     //must be properties!
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string LastName { get; set; }
  }



Edited by IdeaBlade - 01-Mar-2010 at 4:24pm
Back to Top
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Posted: 01-Mar-2010 at 4:37pm
Perfect.  Thank you!  That worked just fine.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down