给定的 LINQ to Collection 查询有什么区别?
我对集合和 IEnumerable 类型感到非常困惑。任何人都可以解释为什么第一个查询是错误的,但第二个查询是正确的-
第一 - 这会产生错误
ConditionFieldCollection conditionColl = (ConditionFieldCollection)Session["ConditionFieldCollection"];
ConditionFieldCollection cnd = new ConditionFieldCollection();
cnd = (from c in conditionColl
where iq.QueryField == c.Expression
select c);
第二 - 工作正常
ConditionFieldCollection conditionColl = (ConditionFieldCollection)Session["ConditionFieldCollection"];
List<ConditionField> cnd = (from c in conditionColl.OfType<ConditionField>()
where iq.QueryField == c.Expression
select c).ToList();
我知道 LINQ 返回 IEnumerable 类型的集合对象,但 ConditionFieldCollection 也是一个 Collection 那么为什么它在编译时给我错误。 Collecton 和 IEnumerable Collection 有什么区别吗??
I am pretty confused in collection and IEnumerable types. Can any one explain why the 1st query is wrong but the 2nd is correct-
1st- this gives error
ConditionFieldCollection conditionColl = (ConditionFieldCollection)Session["ConditionFieldCollection"];
ConditionFieldCollection cnd = new ConditionFieldCollection();
cnd = (from c in conditionColl
where iq.QueryField == c.Expression
select c);
2nd - works fine
ConditionFieldCollection conditionColl = (ConditionFieldCollection)Session["ConditionFieldCollection"];
List<ConditionField> cnd = (from c in conditionColl.OfType<ConditionField>()
where iq.QueryField == c.Expression
select c).ToList();
I know LINQ returns IEnumerable type of collection object but ConditionFieldCollection is also a Collection then why it gives me error at compile time. Is there any difference b/w Collecton and IEnumerable Collection??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试将
IEnumerable
分配给类型为ConditionFieldCollection
的变量 - 这是无效的。可枚举不是集合,当然也不是特定的集合。许多集合允许可枚举构造函数,因此这可能有效:
You are attempting to assign an
IEnumerable<ConditionField>
to a variable with typeConditionFieldCollection
- that can't work. An enumerable is not a collection, and certainly isn't that specific collection.Many collections allow an enumerable constructor, so this may work:
仅仅因为
ConditionFieldCollection
是一个集合,并不意味着它与查询的返回值是同一个集合。这就像说
编辑
根据
ConditionFieldCollection
的工作方式,您可能有两种选择,希望至少其中一种有效。1
2
Just because
ConditionFieldCollection
is a collection does not mean it is the same collection as the return value from the query.It is like saying
Edit
Depending on how
ConditionFieldCollection
works you may have two options, hopefully at least one of them works.1
2