Returning results from DLinq query in Webservice

A

Andrus

I need to return results of anon DLinq query from ASMX Web service.

I tried code below but in this case web service call does not return any
properties.
How to fix ?

Andrus.

[WebService]
public sealed class StockService {

[WebMethod]
public object[] GetProductList(string table, int pageNumber, int
itemsPerPage, string filter, string sortBy, out int totalCount)
{
using (var db = new Database())
{

swithch (Table) {
case "Customer":
totalCount = db.Customer.Count();

var l =
db.Customer.Skip(pageNumber*itemsPerPage).Take(itemsPerPage ).
Select((c) => new { c.Id, c.Name}).ToArray();
return l;

case "Product":
totalCount = db.Product.Count();
var l =
db.Product.Skip(pageNumber*itemsPerPage).Take(itemsPerPage ).
Select((p) => new { p.Id, p.Name, p.Address}).ToArray();
return l;
}
}
}
}
 
P

Peter Morris

The problem is that you are returning object[] which has no properties to
return. If this is WCF you can add a ServiceKnownType attribute to the
interface's method; but it looks to me like a standard webservice in which
case you should create two methods, one for Customer and one for Product
which return Customer[] and Product[] respectively.
 
A

Andrus

Peter,

thank you.
The problem is that you are returning object[] which has no properties to
return. If this is WCF you can add a ServiceKnownType attribute to the
interface's method; but it looks to me like a standard webservice in which
case you should create two methods, one for Customer and one for Product
which return Customer[] and Product[] respectively.

I'm sorry, my code was too simplified.
In real method return property names are not known at design time: user can
select at runtime which columns she wants to
see.
So I must create single generic method.
How to implement this ?
Is it possible/reasonable to convert result from List<TAnonymuous> to
List<Dictionary<string,object>>
so that any property list can serialized and returned as dictionary from
WebService ?

Andrus.
 
P

Peter Morris

If the client is a .NET one you could return a DataSet. If it's not you
could return an XML string. If you switch to WCF you can list lots of
ServiceKnownType and return all kinds of things.
 
A

Andrus

Peter,

thank you.
If the client is a .NET one you could return a DataSet. If it's not you
could return an XML string. If you switch to WCF you can list lots of
ServiceKnownType and return all kinds of things.

Client is SliverLight 2 applicaton. SL does not support DataSet so it is not
possible.
I'm planning to use MONO as server. MONO does not support WCF services so
this is also impossible.
I have code which can use Dictionary list in SilverLight application.
So it may be best to create Dictonary list from XMLin SL.

So there are two solutions:

1. Convert List<TAnonymuous> to XML format which can then converted to
dictionary in Silverlight client.
2. Convert List<TAnonymuous> to Dictionary list.
Hopefully ASMX service can serialize and return this list.

Where to find sample code for those solutions ?

Andrus.
 
P

Peter Morris

At a place I am contracting for we are returning data transfer objects. One
of the guys converted Hessian to work with SilverLight. If you email me
directly I will ask for permission to let you have it if you'd like?
 
A

Andrus

Peter,

I resolved this issue by changing my Web method signature to

public List<object[]> GetProductList(int pageNumber, int itemsPerPage,
string filter, string sortBy, out int totalProductCount, out string[]
columnName)
{}

And creating Dictionary in Silverlight client side:

public IEnumerable<IDictionary> GenerateData(string[] column, object[][]
result) {
for (var i = 0; i < result.GetLength(0); i++) {
var dict = new Dictionary<string, object>();
for (int j = 0; j < column.Length; j++)
dict[column[j]] = result[j];
yield return dict;
}
}

....
//http://devinfra-us.blogspot.com/2008/07/agdatagrid-and-silverlight-datagrid-how.html
InventoryList.DataSource = GenerateData(e.columnName,
e.Result).ToDataSource();

I'm using Bodurow excellent ToDataSource() method.

I'm interesting on looking to other free and open source solutions.

What is Hessian and how to use it ?

Andrus Moor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top