2011-09-07 15 views
26

Vorrei sapere come posso interrogare una matrice di oggetti. Ad esempio, ho un oggetto array come CarList. Quindi CarList [0] mi restituirebbe l'oggetto Car. L'auto ha proprietà Modello e Marca. Ora, voglio usare linq per interrogare l'array CarList per ottenere il Make of a Car il cui modello è "bmw". Ho provato il seguenteInterrogare una matrice di oggetti usando linq

var carMake = from item in CarList where item .Model == "bmw" select s.Make; 

ottengo l'errore

Impossibile trovare un'implementazione del modello di query per il tipo di fonte carlista []

non posso cambiare carlista da matrice a qualcosa come Elenco <> dal momento che CarList si ritira come array da un webservice.

Gentilmente fammi sapere come questo può essere risolto. Sarebbe bello se tu potessi spiegare usando il codice C#.

Grazie in anticipo.

+1

non dovresti selezionare item.Make? –

+1

Tutti, qual è il motivo per votare due volte una domanda generata da un errore di battitura nel codice? cambiando s nell'oggetto e rimuovendo lo spazio prima. Modello è l'unica soluzione di cui aveva bisogno. –

risposta

52

Add:

using System.Linq; 

alla parte superiore del file.

E poi:

Car[] carList = ... 
var carMake = 
    from item in carList 
    where item.Model == "bmw" 
    select item.Make; 

o se si preferisce la sintassi fluente:

var carMake = carList 
    .Where(item => item.Model == "bmw") 
    .Select(item => item.Make); 

cose da prestare attenzione a:

  • L'utilizzo di item.Make nella clausola select invece se s.Make come nel tuo codice.
  • Hai uno spazio tra item e .Model nella vostra where clausola
+1

non ha un array Car [] chiamato CarList? –

+0

@Davide Piras, oh si. Hai ragione. Non ho letto attentamente. Grazie per aver individuato questo. Aggiornamento della mia risposta immediatamente. –

0

Il modo migliore per farlo:

ContexteDAO.ContexteDonnees un'istanza di un nuovo contesto.

public static Destination[] Rechercher(string aCodeDestination, string aDénomination, string aVille, string aPays, Single aLatitude, Single aLongitude, string aContinent, 
              string aZoneGeo, string aRelief,string aObservation) 
    { 
     IEnumerable<Destination> DestinationRecherche; 

     DestinationRecherche = ContexteDAO.ContexteDonnees.Destinations; 

     if(!string.IsNullOrEmpty(aCodeDestination)) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a=>a.CodeDestination.Contains(aCodeDestination)); 
     } 
     if (!string.IsNullOrEmpty(aDénomination)) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a => a.Dénomination.Contains(aDénomination)); 
     } 
     if (!string.IsNullOrEmpty(aVille)) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a => a.Ville.Contains(aVille)); 
     } 
     if (!string.IsNullOrEmpty(aPays)) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a => a.Pays.Contains(aPays)); 
     } 
     if (aLatitude != 0) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a => a.Latitude.Equals(aLatitude)); 
     } 
     if (aLongitude != 0) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a => a.Longitude.Equals(aLongitude)); 
     } 
     if (!string.IsNullOrEmpty(aContinent)) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a => a.Continent.Contains(aContinent)); 
     } 
     if(!string.IsNullOrEmpty(aZoneGeo)) 
     {    DestinationRecherche = DestinationRecherche.Where(a => a.ZoneGeographique.Contains(aZoneGeo)); 
     }   
     if(!string.IsNullOrEmpty(aRelief)) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a =>a.Relief.Contains(aRelief)); 
     } 
     if (!string.IsNullOrEmpty(aObservation)) 
     { 
      DestinationRecherche = DestinationRecherche.Where(a => a.ObservationsDestination.Contains(aObservation)); 
     } 
     return DestinationRecherche.ToArray(); 
    } 
Problemi correlati