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??
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: