2011-09-07 71 views
26

Tôi muốn biết làm thế nào tôi có thể truy vấn một mảng đối tượng. Ví dụ tôi có một đối tượng mảng như CarList. Vì vậy, CarList [0] sẽ trả lại cho tôi đối tượng Car. Xe có tính chất Model và Make. Bây giờ, tôi muốn sử dụng LINQ để truy vấn mảng CarList để có được Make of a Car có Model là "bmw". Tôi đã thử các sauTruy vấn mảng đối tượng bằng cách sử dụng linq

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

tôi nhận được lỗi

không thể tìm thấy một thực hiện các mô hình truy vấn cho loại nguồn CarList []

tôi không thể thay đổi CarList từ mảng tới một cái gì đó như List <> kể từ khi CarList được retured cho tôi như là mảng từ một webservice.

Vui lòng cho tôi biết cách giải quyết vấn đề này. Sẽ là tuyệt vời nếu bạn có thể giải thích bằng cách sử dụng mã C#.

Xin cảm ơn trước.

+1

bạn không nên chọn mục.Make? –

+1

Tất cả, lý do gì để bỏ phiếu hai lần một câu hỏi được tạo ra bởi lỗi đánh máy trong mã? thay đổi s vào mục và loại bỏ không gian trước .Model là giải pháp duy nhất anh cần. –

Trả lời

52

Add:

using System.Linq; 

để phía trên cùng của tập tin của bạn.

Và sau đó:

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

hoặc nếu bạn thích cú pháp thông thạo:

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

Những điều cần chú ý đến:

  • Việc sử dụng item.Make trong mệnh đề select thay nếu s.Make như trong mã của bạn.
  • Bạn có một khoảng trắng giữa item.Model trong where khoản của bạn
+1

anh ta có một mảng Car [] có tên CarList không? –

+0

@Davide Piras, oh vâng. Bạn nói đúng. Tôi không đọc kỹ. Cảm ơn vì đã phát hiện ra điều này. Cập nhật câu trả lời của tôi ngay lập tức. –

0

Cách tốt nhất để làm điều đó:

ContexteDAO.ContexteDonnees tạo một bối cảnh mới.

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(); 
    } 
Các vấn đề liên quan