查询使用LINQ对象数组 [英] Query an object array using linq

查看:243
本文介绍了查询使用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。

  • 您有项目。型号,其中条款

  • The usage of item.Make in the select clause instead if s.Make as in your code.
  • You have a whitespace between item and .Model in your where clause

这篇关于查询使用LINQ对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆