使用linq查询对象数组

我想知道如何查询对象数组。 例如,我有一个像CarList这样的数组对象。 所以CarList [0]会把对象Car给我。 汽车有属性Model和Make。 现在,我想使用linq查询数组CarList以获取模型名为“bmw”的Make of a Car。 我尝试了以下内容

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

我收到了错误

无法找到源类型CarList []的查询模式的实现

我无法将CarList从数组更改为List 之类的东西,因为CarList从Web服务中作为数组返回到我。

请告诉我这是如何解决的。 如果您可以使用C#代码解释,那将会很棒。

提前致谢。

加:

 using System.Linq; 

到你的文件的顶部。

然后:

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

或者如果您更喜欢流利的语法:

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

需要注意的事项:

  • 如果s.Make在代码中,则使用item.Makeselect子句中。
  • where子句中, 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 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(); }