新手LINQ问题 [英] Newbie LINQ Question

查看:50
本文介绍了新手LINQ问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码摘录...

--- top ---


列表< PersonPersons =新列表< Person> ();

Persons.Add(新人(彼得,28,珀斯));

Persons.Add(新人(马修) ;,31,Bundaberg));

Persons.Add(new Person(Cathryn,36,Perth));

//这是有效的

IEnumerable< Personp1 =来自人的操作

其中op.City ==" Perth"

select op;


//但是这不起作用

列表< Personp2 =来自人的操作

其中op.City =="珀斯

选择操作;

---底部---

编译器错误消息是:编译器错误消息:CS0266:不能

隐式转换类型

''System.Collections.Generic.IEnumerable< ASP.linq_l inqgrouping_aspx.Person>''



''System.Collections.Generic.List< ASP.l inq_linqgrou ping_aspx.Person>''。

存在显式转换(你是否错过演员表?)


我正试图不进入使用var的习惯,因为我在某处(MSDN)读到它会使你的代码对其他人更难以理解,

我倾向于同意。


有人可以告诉我我做错了吗?


提前致谢,

Damien

Hi, I have the following code extract...
---top---

List<PersonPersons = new List<Person>();
Persons.Add(new Person("Peter", 28,"Perth"));
Persons.Add(new Person("Matthew", 31, "Bundaberg"));
Persons.Add(new Person("Cathryn", 36, "Perth"));
//This works
IEnumerable<Personp1 = from op in Persons
where op.City=="Perth"
select op;

//Tut this doesn''t work
List<Personp2 = from op in Persons
where op.City == "Perth"
select op;
---bottom---
The compiler error message is: Compiler Error Message: CS0266: Cannot
implicitly convert type
''System.Collections.Generic.IEnumerable<ASP.linq_l inqgrouping_aspx.Person>''
to
''System.Collections.Generic.List<ASP.linq_linqgrou ping_aspx.Person>''.
An explicit conversion exists (are you missing a cast?)

I''m trying to not get into the habit of using var because I read
somewhere (MSDN) that it makes your code more unreadable to others,
which I tend to agree with.

Can someone please tell me what I''m doing wrong?

Thanks in advance,
Damien

推荐答案

6月20日,8:51 * am,damiensaw ... @ yahoo.com.au

< damiensaw。 .. @ yahoo.com.auwrote:


< snip>
On Jun 20, 8:51*am, "damiensaw...@yahoo.com.au"
<damiensaw...@yahoo.com.auwrote:

<snip>

我正试图不进入使用var的习惯是因为我在某处(MSDN)读到它会使你的代码对其他人更难以理解,

我倾向于同意。
I''m trying to not get into the habit of using var because I read
somewhere (MSDN) that it makes your code more unreadable to others,
which I tend to agree with.



我个人发现var *增加*可读性而不是降低它的价值,但这是个人的事情。

I personally find that var *increases* readability rather than
reducing it, but it''s a personal thing.


有人可以告诉我,我做错了吗?
Can someone please tell me what I''m doing wrong?



正是编译器所说的 -

RHS上的表达式的结果是IEnumerable< Person>,而不是List< Person>。

如果你想在List中,你应该在

查询表达式的结果上调用ToList。


提醒你,如果你只是*使用在哪里子句代码将

可能更简单如下:

列表< Personp2 = people.Where(person =>

person.City == 珀斯.ToList();


(请注意,我已将人员更改为人员以跟随命名

惯例和让阅读更舒服:)


Jon

Exactly what the compiler says - the result of the expression on the
RHS is IEnumerable<Person>, not List<Person>.
If you want it in a List, you should call ToList on the result of the
query expression.

Mind you, if you''re *just* using a "where" clause the code would
probably be simpler as:
List<Personp2 = people.Where(person =>
person.City=="Perth").ToList();

(Note that I''ve changed "Persons" to "people" to follow naming
conventions and make for more comfortable reading :)

Jon


da ********** @ yahoo.com.au 写道:

我有以下代码摘录...


---顶部---


列表< PersonPersons =新列表< ;人>();

Persons.Add(新人(彼得,28,珀斯));

Persons.Add(new Person( Matthew,31,Bundaberg));

Persons.Add(new Person(" Cathryn",36," Perth));


//这是有效的

IEnumerable< Person p1 =来自op in Persons

其中op.City ==" Perth"

select op;


// Tut这不起作用

列表< Personp2 =来自op in Persons

其中op.City ==" Perth"
select op ;

---底部---


编译器错误消息是:编译器错误消息:CS0266:无法隐含地
转换类型

''System.Collections.Generic.IEnumerable< ASP.linq_l inqgrouping_aspx.Person>''

to

''系统。 Collections.Generic.List< ASP.linq_linqgrou ping_aspx.Person>''。

存在显式转换(你是否错过演员表?)


我'我试图不养成使用var的习惯,因为我在某个地方(MSDN)读到了它使你的代码对其他人来说更难以理解,

whic我倾向于同意。


有人可以告诉我我做错了什么吗?
Hi, I have the following code extract...
---top---

List<PersonPersons = new List<Person>();
Persons.Add(new Person("Peter", 28,"Perth"));
Persons.Add(new Person("Matthew", 31, "Bundaberg"));
Persons.Add(new Person("Cathryn", 36, "Perth"));
//This works
IEnumerable<Personp1 = from op in Persons
where op.City=="Perth"
select op;

//Tut this doesn''t work
List<Personp2 = from op in Persons
where op.City == "Perth"
select op;
---bottom---
The compiler error message is: Compiler Error Message: CS0266: Cannot
implicitly convert type
''System.Collections.Generic.IEnumerable<ASP.linq_l inqgrouping_aspx.Person>''
to
''System.Collections.Generic.List<ASP.linq_linqgrou ping_aspx.Person>''.
An explicit conversion exists (are you missing a cast?)

I''m trying to not get into the habit of using var because I read
somewhere (MSDN) that it makes your code more unreadable to others,
which I tend to agree with.

Can someone please tell me what I''m doing wrong?



来自op.City ==" Perth" select op;''part isn'n

a List< Person but an IEnumerable。如果你想要一个List< Person>,

,你应该这样做:


List< Personp2 =(来自op in Persons

其中op.City ==" Perth"

select op).ToList();


背景是linq到对象,这是你使用的是什么

''人'是一个IEnumerable,而不是一个包含Linq

提供者的IQueryable,只是做一个对你的人有收益的foreach 。


所以查询等于这段代码:


foreach(人物操作人员)

[

if(op.City!=" Perth")

{

继续;

}

收益率返回操作;

}


只生成一个IEnumerable< PersonEnumerator,由

创建编译器。


FB


-

------------- -------------------------------------------------- ---------

LLBLGen Pro的首席开发人员,富有成效用于.NET的O / R映射器

LLBLGen Pro网站: http:// www .llblgen.com

我的.NET博客: http ://weblogs.asp.net/fbouma

Microsoft MVP(C#)

--------------- -------------------------------------------------- -------

the ''from op in Persons where op.City == "Perth" select op;'' part isn''t
a List<Personbut an IEnumerable. If you want to have a List<Person>,
you should do:

List<Personp2 = (from op in Persons
where op.City == "Perth"
select op).ToList();

The background is that linq to objects, which is what you''re using as
''Persons'' is an IEnumerable and not an IQueryable containing a Linq
provider, simply does a foreach with a yield over your persons.

so the query is equal to this code:

foreach(Person op in Persons)
[
if(op.City!="Perth")
{
continue;
}
yield return op;
}

which simply produces an IEnumerable<PersonEnumerator, created by the
compiler.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------


Jon Skeet [C#MVP]写道:
Jon Skeet [C# MVP] wrote:

我个人觉得var增加了可读性而不是降低它的价值,但这是个人的事情。
I personally find that var increases readability rather than
reducing it, but it''s a personal thing.



你在这方面的表现相当不错......

http://www.codinghorror.com/blog/archives/001136.html


问候蒂姆。

-

You are in pretty good company on that score...

http://www.codinghorror.com/blog/archives/001136.html

Regards Tim.
--


这篇关于新手LINQ问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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