Typed result set possible?
Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1662
Printed Date: 21-Sep-2025 at 2:12am
Topic: Typed result set possible?
Posted By: mikewishart
Subject: Typed result set possible?
Date 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!
|
Replies:
Posted By: IdeaBlade
Date 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:
- 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).
- 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.
- 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; } }
file:///C:%5CDOCUME%7E1%5CGregD%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml -
|
Posted By: mikewishart
Date Posted: 01-Mar-2010 at 4:37pm
Perfect. Thank you! That worked just fine.
|
|