查询使用LINQ对象数组 [英] Query an object array using linq
问题描述
我想知道我怎么可以查询对象的数组。比如我有像卡洛斯数组对象。所以卡洛斯[0]将返回我的对象车。汽车具有属性模型和制作。现在,我想用LINQ查询数组,卡洛斯拿到车的型号是说宝马的品牌。我尝试以下
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
var carMake = from item in CarList where item .Model == "bmw" select s.Make;
我得到的错误
找不到源类型卡洛斯查询模式的实现[]
Could not find an implementation of the query pattern for source type CarList[]
我不能从卡洛斯阵列更改为类似清单<>因为卡洛斯被retured我从web服务阵列
I cannot change CarList from array to something like List<> since CarList is retured to me as array from a webservice.
请让我知道如何可以解决的。将是巨大的,如果你能解释一下使用C#code。
Kindly let me know how this can be solved. Would be great if you can explain using C# code.
先谢谢了。
推荐答案
地址:
using System.Linq;
到文件的顶部。
to the top of your file.
和则:
Car[] carList = ...
var carMake =
from item in carList
where item.Model == "bmw"
select item.Make;
或者如果你preFER流利的语法:
or if you prefer the fluent syntax:
var carMake = carList
.Where(item => item.Model == "bmw")
.Select(item => item.Make);
事情要注意:
-
item.Make
中使用的选择
条款,而不是如s.Make
作为您code。 - 您有
项目
和。型号
在,其中$之间的空白C $ C>条款
- The usage of
item.Make
in theselect
clause instead ifs.Make
as in your code. - You have a whitespace between
item
and.Model
in yourwhere
clause
这篇关于查询使用LINQ对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!