I would like to know how can I query an array of objects. For example I have an array object like CarList. So CarList[0] would return me the object Car. Car has properties Model and Make. Now, I want to use linq to query the array CarList to get the Make of a Car whose Model is say "bmw". I tried the following
我想知道如何查询对象数组。例如,我有一个像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;
I get the error
我收到了错误
Could not find an implementation of the query pattern for source type CarList[]
无法找到源类型CarList []的查询模式的实现
I cannot change CarList from array to something like List<> since CarList is retured to me as array from a webservice.
我无法将CarList从数组更改为List <>之类的东西,因为CarList从Web服务中作为数组返回到我。
Kindly let me know how this can be solved. Would be great if you can explain using C# code.
请告诉我这是如何解决的。如果您可以使用C#代码解释,那将会很棒。
Thanks in advance.
提前致谢。
2 个解决方案
#1
54
Add:
加:
using System.Linq;
to the top of your file.
到你的文件的顶部。
And then:
接着:
Car[] carList = ...
var carMake =
from item in carList
where item.Model == "bmw"
select item.Make;
or if you prefer the fluent syntax:
或者如果您更喜欢流利的语法:
var carMake = carList
.Where(item => item.Model == "bmw")
.Select(item => item.Make);
Things to pay attention to:
需要注意的事项:
- The usage of
item.Make
in theselect
clause instead ifs.Make
as in your code. - 如果s.Make在代码中,则使用item.Make在select子句中。
- You have a whitespace between
item
and.Model
in yourwhere
clause - 在where子句中,item和.Model之间有一个空格
#2
0
The best way to do it:
最好的方法:
ContexteDAO.ContexteDonnees instantiate a new context.
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();
}
#1
54
Add:
加:
using System.Linq;
to the top of your file.
到你的文件的顶部。
And then:
接着:
Car[] carList = ...
var carMake =
from item in carList
where item.Model == "bmw"
select item.Make;
or if you prefer the fluent syntax:
或者如果您更喜欢流利的语法:
var carMake = carList
.Where(item => item.Model == "bmw")
.Select(item => item.Make);
Things to pay attention to:
需要注意的事项:
- The usage of
item.Make
in theselect
clause instead ifs.Make
as in your code. - 如果s.Make在代码中,则使用item.Make在select子句中。
- You have a whitespace between
item
and.Model
in yourwhere
clause - 在where子句中,item和.Model之间有一个空格
#2
0
The best way to do it:
最好的方法:
ContexteDAO.ContexteDonnees instantiate a new context.
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();
}