CollectionBase and IQueryable
I am following some existing code and trying to implement it the same way for this new class and the behavior.
however the problem is that I will not be able to query it using IQueryable because it is a CollectionBase:
Code:
public class ValidityPeriodItemCollection : CollectionBase
{
public ValidityPeriodItem this[int index]
{
get { return (ValidityPeriodItem)List[index]; }
set { List[index] = value; }
}
public ValidityPeriodItem this[Guid id]
{
get
{
foreach (ValidityPeriodItem item in this)
{
if (item.Id == id)
return item;
}
return null;
}
}
public int Add(ValidityPeriodItem item)
{
return List.Add(item);
}
}
what I want to be able to do is simply query this using IQueryable, so I can filter and select a ValidityPeriodItem
how can I do this with the current implementation?
Re: CollectionBase and IQueryable
woops....
Enumerable.Cast<ValidityPeriodItem>(this.ValidityPeriods);
Re: CollectionBase and IQueryable
Why are you using CollectionBase at all? CollectionBase is one of the many classes that should not be used from .NET 2.0 onwards, with the introduction of generics. You should be using Collection<T>. Because Collection<T> implements IEnumerable<T>, which CollectionBase obviously doesn't, you wouldn't need the Cast.
Apart from that, Cast is an extension method, so it should be used like this:
Code:
this.ValidityPeriods.Cast<ValidityPeriodItem>();
Re: CollectionBase and IQueryable
thanks. if it were my choice, then i wouldnt be using it but unfortunately its how the code already is. enterprise solution etc... etc...