471,349 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Creating an Extended Generic List Collection

I simply want to extend the List<Tobject 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<Tmethod. 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
Jul 16 '08 #1
3 4642
"daokfella" <jj******@hotmail.comwrote in message
news:f4**********************************@59g2000h sb.googlegroups.com...
>I simply want to extend the List<Tobject 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<Tmethod. Is that possible? If so, how do I do
it?
That's what Extension Methods are for:

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

Now you can do

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

Jul 16 '08 #2
Thanks, I figured out the extension method. My problem, however, is
that a generic List<Twill not cast to my EntityList<Teven though
my class simply inherits List<Tand 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<Tsource, int VirtualCount) :
this(source)
{
this.VirtualCount = VirtualCount;
}
public EntityList(IQueryable<Tsource)
{
using (IEnumerator<Tenumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
this.Add(enumerator.Current);
}
}
}
}

and my extender to this:

public static class EntityExtensions
{
public static EntityList<TSourceToEntityList<TSource>(this
IQueryable<TSourcesource)
{
return new EntityList<TSource>(source);
}
public static EntityList<TSourceToEntityList<TSource>(this
IQueryable<TSourcesource, int VirtualCount)
{
return new EntityList<TSource>(source, VirtualCount);
}
}
Jul 16 '08 #3
"daokfella" <jj******@hotmail.comwrote in message
news:3c**********************************@x41g2000 hsb.googlegroups.com...
[...] a generic List<Twill not cast to my EntityList<Teven 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<TlistToConvert)
{
//Process listToConvert and return an EntityList<T>
}

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

Jul 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michael K. Walter | last post: by
8 posts views Thread by JAL | last post: by
6 posts views Thread by MikeSwann | last post: by
reply views Thread by crazyone | last post: by
4 posts views Thread by =?Utf-8?B?QkogU2FmZGll?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.