LINQ unable to cast object

Résolu
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 - Modifié le 26 nov. 2018 à 21:02
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 - 27 nov. 2018 à 07:18
Bonjour,

J'ai la class Info
public    Class info   
          public name as string
           public day as string
           public  thedate as string
end class
et 
public listA as new list of(info)
private function FillListA as list of(info)
  'bla bla
end function
public function Query  as  list of(info)
  listA = FillListA()
  dim  listinfo  = From evenement  in listA where evenement.name .contains("toto")  and evenement.day = "monday" and evenement.thedate = " 4 july" select evenement
 return listinfo
end function</code >

déjà une erreur  a la ligne  <code>return listinfo

le correcteur marque
Return CType(listinfo, Global.System.Collections.Generic.List(Of info))
Mais quand je lance j'obtiens l'erreur
Unable to cast object of type 'WhereSelectListIterator`2[info,info]' to type 'System.Collections.Generic.List`1info]'.

merci d'avance

3 réponses

Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
26 nov. 2018 à 20:59
Bonsoir
merci de poster ton code lisiblement en appliquant la procédure décrite ici
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
26 nov. 2018 à 22:14
Bonsoir
On ne peut pas modifier le post original l'icon crayon "modifier" n'apparait pas
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
26 nov. 2018 à 22:07
public    Class info   
          public name as string
           public day as string
           public  thedate as string
end class

et
public listA as new list of(info)
private function FillListA as list of(info)
  'bla bla
end function

public function Query  as  list of(info)
  listA = FillListA()
  dim  listinfo  = From evenement  in listA where evenement.name .contains("toto")  and evenement.day = "monday" and evenement.thedate = " 4 july" select evenement
 return listinfo
end function


déjà une erreur a la ligne
return listinfo


le correcteur marque
Return CType(listinfo, Global.System.Collections.Generic.List(Of info))
Mais quand je lance j'obtiens l'erreur
Unable to cast object of type 'WhereSelectListIterator`2[info,info]' to type 'System.Collections.Generic.List`1info]'.
0
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
27 nov. 2018 à 07:18
Une requête linq retourne un IEnumerable(of. ) comme ta méthode est définie comme retournant une List(of ) listinfo doit être une List.
Il suffit donc de caster listinfo en List:
Return listinfo.ToList()
à condition bien sûr que ListA contienne des « info »
0
Rejoignez-nous