Jump to content
RJ90

WebService - Problema (List)

Recommended Posts

RJ90

Boas!

Estou aqui com um problema a retornar uma lista.

Tenho o seguinte:

public List<Victim> GetVictims()
{
using (DataClassesDataContext dcdc = new DataClassesDataContext())
        {
	var victims = from victim in dcdc.Victims
                              select new { Name = victim.Name, Gender = victim.Gender };

                return (List<Victim>)victims;
}
}

Erro:

Unable to cast object of type 'System.Data.Linq.DataQuery`1[<>f__AnonymousType0`2[system.String,System.String]]' to type 'System.Collections.Generic.List`1[PROJECTO.Web.Victim]'.

Se usar:

public List<Victim> GetVictims()
{
using (DataClassesDataContext dcdc = new DataClassesDataContext())
        {
	var victims = from victim in dcdc.Victims
                              select new Victim { Name = victim.Name, Gender = victim.Gender };

                return victims.ToList();
}
}

Não posso usar a Classe Victim no WebService.

O problema está em basicamente converter um tipo anónimo para lista.

Agradecia ajuda.

Cumps,


Samsung 700Z5A-S01PT Samsung Ominia W

ᥡ What Else? ᥡ

Share this post


Link to post
Share on other sites
Caça

Tens de configurar o WebService para que ele aceite colecções do tipo List


Pedro Martins

Não respondo a duvidas por PM

Share this post


Link to post
Share on other sites
RJ90

Tens de configurar o WebService para que ele aceite colecções do tipo List

Como?


Samsung 700Z5A-S01PT Samsung Ominia W

ᥡ What Else? ᥡ

Share this post


Link to post
Share on other sites
Caça

Antes de mais, nunca deves retornar nenhuma lista sem tipo("AnonymousType").

Para configurares, deves ir a projecto onde referenciaste o Serviço -> Tecla direita -> Configurar

E principio tens seleccionado na combobox Collection type o item "System.Array", altera para "System.Collections.Generic.List"


Pedro Martins

Não respondo a duvidas por PM

Share this post


Link to post
Share on other sites
RJ90

Na interface fiz assim:

[DataContract]
public class WSVictim
{
[DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Gender { get; set; }
}

Na classe do WebService:

public List<WSVictim> GetVictims()
{
using (DataClassesDataContext dcdc = new DataClassesDataContext())
{
	var victims = from victim in dcdc.Victims
                              select new WSVictim { Name = victim.Name, 
		      Gender = victim.Gender.SexualGender };

                return victims.ToList();
}
}

Feito.

Obrigado!


Samsung 700Z5A-S01PT Samsung Ominia W

ᥡ What Else? ᥡ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.