Creating an Extended Generic List Collection

D

daokfella

I simply want to extend the List<T> object to include a property
called VirtualCount. Basically, the list will hold my paged records,
but the VirtualCount property will tell me how many records total are
in the result set. It seems easy enough to create my class:

public class EntityList<T> : List<T>
{
public int VirtualCount { get; set; }
}

I was wondering however if I can extend LINQ IQueryable so I can
provide a ToEntityList<T> method. Is that possible? If so, how do I do
it?

Right now, I'm just casting after the ToList():
var results = (EntityList<Permission>)
(MyDataContext.Permissions.Where(a => a.ApplicationID ==
ApplicationID).ToList());
I would simply like to do this:
var results = MyDataContext.Permissions.Where(a => a.ApplicationID ==
ApplicationID).ToEntityList();

Thanks,

Jason
 
A

Alberto Poblacion

daokfella said:
I simply want to extend the List<T> object to include a property
called VirtualCount. Basically, the list will hold my paged records,
but the VirtualCount property will tell me how many records total are
in the result set. It seems easy enough to create my class:

public class EntityList<T> : List<T>
{
public int VirtualCount { get; set; }
}

I was wondering however if I can extend LINQ IQueryable so I can
provide a ToEntityList<T> method. Is that possible? If so, how do I do
it?

That's what Extension Methods are for:

public static class whetever
{
public static EntityList<T> ToEntityList<T>(this IQueryable iq)
{
return (EntityList<T>)(iq.ToList());
}
}

Now you can do

var results = MyDataContext.Permissions.Where(...).ToEntityList();
 
D

daokfella

Thanks, I figured out the extension method. My problem, however, is
that a generic List<T> will not cast to my EntityList<T> even though
my class simply inherits List<T> and only adds a single property.
Perhaps you may know why. In the interim, I just changed my class to
this:

public class EntityList<T> : List<T>
{
public int VirtualCount { get; set; }
public EntityList()
{
}
public EntityList(IQueryable<T> source, int VirtualCount) :
this(source)
{
this.VirtualCount = VirtualCount;
}
public EntityList(IQueryable<T> source)
{
using (IEnumerator<T> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
this.Add(enumerator.Current);
}
}
}
}

and my extender to this:

public static class EntityExtensions
{
public static EntityList<TSource> ToEntityList<TSource>(this
IQueryable<TSource> source)
{
return new EntityList<TSource>(source);
}
public static EntityList<TSource> ToEntityList<TSource>(this
IQueryable<TSource> source, int VirtualCount)
{
return new EntityList<TSource>(source, VirtualCount);
}
}
 
A

Alberto Poblacion

daokfella said:
[...] a generic List<T> will not cast to my EntityList<T> even though
my class simply inherits List<T>

No, it works the other way around: casting from the child class to the
parent class always works, but casting from the parent to the child will
only work if at that point the parent already contains an instance of the
child.
To work around this limitation you have to provide your own conversion
instructions, which you already did in the constructor. If you want a
"clean" way to do this, you can provide an implicit or explicit conversion
operator inside your class:

public static explicit operator EntityList<T> (List<T> listToConvert)
{
//Process listToConvert and return an EntityList<T>
}

After adding this operator to your EntityList<T> class, you can cast from a
List<T> to your class. If you use "implicit" instead of "explicit", you can
do a direct assignment between both classes without writing a cast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top