return linq result via web service
I'm trying to use Linq in a webservice that returns a data from a query. As the data return from Linq to Sql is IEnumerable and it's not possible to easily get a Dataset.what is the best format to return data?
DataClassesDataContext Dac = new DataClassesDataContext();
Dac.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionCs"].ConnectionString;
var query = from record in Dac.RetrieveWorksh开发者_如何学编程ops(WorkshopCode, Name) select record;
Web-service proxies instantiate collections as arrays, so I've gotten into the habit of defining 'data transfer objects' and returning IEnumerable's of those DTO's. In your case, You could define a WorkshopDTO object which contained only the public properties you wanted to return and have your LINQ query construct them based on each record it reads. Then your client-proxy will see an array of WorkshopDTO's to manipulate.
You can populate your data into an object and return a list of that object in Web Service.
Example:
public class WorkshopEntity
{
string Name { get; set; }
string Location { get; set; }
}
List<WorkshopEntity> workshopList = (from record in Dac.RetrieveWorkshops(WorkshopCode, Name) select new WorkshopEntity { Name=record.name, Location=record.Location }).ToList();
return query.ToArray();
but remember to add [Serializable]
Attribiute at top of your WorkshopEntity
class.
if your have complex type properties in WorkshopEntity
inform me because you should do more.
精彩评论