2011-09-07 15 views
26

Me gustaría saber cómo puedo consultar una matriz de objetos. Por ejemplo, tengo un objeto de matriz como CarList. Entonces CarList [0] me devolvería el objeto Car. El auto tiene propiedades Modelo y Marca. Ahora, quiero usar linq para consultar la matriz CarList para obtener el Make of a Car cuyo modelo es decir "bmw". He intentado lo siguienteConsultar una matriz de objetos utilizando linq

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

me sale el error

No se pudo encontrar una implementación del patrón de consulta para el tipo de origen carlista []

No puedo cambiar carlista de la matriz a algo como List <> ya que CarList me pertenece como matriz desde un servicio web.

Por favor, hágamelo saber cómo se puede resolver esto. Sería genial si puedes explicar usando el código C#.

Gracias de antemano.

+1

¿no debería seleccionar el artículo.¿Hacer? –

+1

Todo, ¿cuál es el motivo para votar dos veces una pregunta generada por un error tipográfico en el código? cambiando s en elemento y eliminando espacio antes. El modelo es la única solución que necesitaba. –

Respuesta

52

Añadir:

using System.Linq; 

a la parte superior de su archivo.

Y luego:

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

o si lo prefiere la sintaxis fluidez:

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

cosas que prestar atención a:

  • El uso de item.Make en la cláusula select vez si s.Make como en su código.
  • Usted tiene un espacio en blanco entre item y .Model en su where cláusula
+1

¿no tiene una matriz Car [] llamada CarList? –

+0

@Davide Piras, oh sí. Estás en lo correcto. No leí con cuidado. Gracias por descubrir esto Actualizando mi respuesta de inmediato. –

0

La mejor manera de hacerlo:

ContexteDAO.ContexteDonnees una instancia de un nuevo contexto.

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(); 
    } 
Cuestiones relacionadas