user486523
user486523

Reputation:

How can I get the following collection to return the original type?

I have the following method which I cast to ICloseable to expose the property I want to check against.

Problem is that the return type is now a Set of type ICloseable.

Is there a way of checking whether T is closed without it returning a Set of type ICloseable?

if (typeof(ICloseable).IsAssignableFrom(typeof(T)))
{
    return base.Set<T>().Where(n => !((ICloseable)n).Closed);
}

Upvotes: 0

Views: 86

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500835

As IQueryable<T> is covariant in T, you could try:

if (typeof(ICloseable).IsAssignableFrom(typeof(T)))
{
    IQueryable<ICloseable> closeables = (IQueryable<ICloseable>) base.Set<T>();
    return closeables.Where(n => !n.Closed).Cast<T>();
}

I don't know whether the Cast will definitely work, but it's worth a try.

Upvotes: 1

Related Questions