给定的 LINQ to Collection 查询有什么区别?

发布于 2024-10-20 12:16:49 字数 949 浏览 1 评论 0原文

我对集合和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-10-27 12:16:49

您尝试将 IEnumerable 分配给类型为 ConditionFieldCollection 的变量 - 这是无效的。可枚举不是集合,当然也不是特定的集合。

许多集合允许可枚举构造函数,因此这可能有效:

ConditionFieldCollection cnd = new ConditionFieldCollection(
    from c in conditionColl
    where iq.QueryField == c.Expression
    select c);

You are attempting to assign an IEnumerable<ConditionField> to a variable with type ConditionFieldCollection - 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 cnd = new ConditionFieldCollection(
    from c in conditionColl
    where iq.QueryField == c.Expression
    select c);
隱形的亼 2024-10-27 12:16:49

仅仅因为 ConditionFieldCollection 是一个集合,并不意味着它与查询的返回值是同一个集合。

这就像说

class Mammal{}
class Dog : Mammal {}
class Cat : Mammal {}

Dog d = new Dog();
Cat c = new Cat();
d = c; // Wrong, even though they are both mammals they are not the same kind of mammal.

编辑
根据 ConditionFieldCollection 的工作方式,您可能有两种选择,希望至少其中一种有效。

1

ConditionFieldCollection cnd = new ConditionFieldCollection(); 
cnd.AddRange(from c in conditionColl
             where iq.QueryField == c.Expression
             select c);

2

ConditionFieldCollection cnd = 
    new ConditionFieldCollection(from c in conditionColl
                                 where iq.QueryField == c.Expression
                                 select c);

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

class Mammal{}
class Dog : Mammal {}
class Cat : Mammal {}

Dog d = new Dog();
Cat c = new Cat();
d = c; // Wrong, even though they are both mammals they are not the same kind of mammal.

Edit
Depending on how ConditionFieldCollection works you may have two options, hopefully at least one of them works.

1

ConditionFieldCollection cnd = new ConditionFieldCollection(); 
cnd.AddRange(from c in conditionColl
             where iq.QueryField == c.Expression
             select c);

2

ConditionFieldCollection cnd = 
    new ConditionFieldCollection(from c in conditionColl
                                 where iq.QueryField == c.Expression
                                 select c);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文