RJ90 0 Posted April 5, 2011 Report Share Posted April 5, 2011 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? ᥡ Link to post Share on other sites
Caça 17 Posted April 5, 2011 Report Share Posted April 5, 2011 Tens de configurar o WebService para que ele aceite colecções do tipo List Pedro Martins Não respondo a duvidas por PM Link to post Share on other sites
RJ90 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Tens de configurar o WebService para que ele aceite colecções do tipo List Como? ◣ Samsung 700Z5A-S01PT ◥ ◣ Samsung Ominia W ◥ ᥡ What Else? ᥡ Link to post Share on other sites
Caça 17 Posted April 5, 2011 Report Share Posted April 5, 2011 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 Link to post Share on other sites
RJ90 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 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? ᥡ Link to post Share on other sites
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now