2011-09-07 13 views
26

オブジェクトの配列をどのようにクエリできますか。たとえば、私はCarListのような配列オブジェクトを持っています。 CarList [0]は私にオブジェクトCarを返します。車はプロパティーModelとMakeを持っています。今、私はlinqを使って配列CarListに問い合わせて、モデルが "bmw"と言う車を作るのです。私は、次のlinqを使用してオブジェクト配列を照会します

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

が、私はエラーを取得しようとした

ソースタイプCarListのクエリパターンの実装を見つけることができませんでした[]

私が何かに配列からCarListを変更することはできません

リスト<>のように、CarListはウェブサービスからの配列として私に返却されるからです。

どうすればこの問題を解決できるか教えてください。あなたはC#のコードを使用して説明することができれば素晴らしいだろう。

ありがとうございます。

+1

アイテムを選択しないでください。 –

+1

すべての理由で、コードの入力ミスによって生成された質問を2度投票する理由は何ですか? .Modelが彼が必要とする唯一の解決策である前に、sを項目に変更し、スペースを削除する。 –

答えて

52

追加:ファイルの先頭に

using System.Linq; 

そして:

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

またはあなたが流暢な構文を好む場合:代わりにselect句で

  • item.Makeの使用:に注意を払うために

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

    物事をコードの場合はs.Makeとなります。

  • あなたのwhere
+1

彼はCarListというCar []配列を持っていませんか? –

+0

@Davide Piras、そうです。あなたは正しいです。私は注意深く読まなかった。これを見つけていただきありがとうございます。すぐに私の答えを更新する。 –

0

でそれを行うための最善の方法をitem.Modelの間に空白を含める:

ContexteDAO.ContexteDonneesは、新しいコンテキストをインスタンス化します。

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(); 
    } 
関連する問題