473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4738
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
3
by: snesbit | last post by:
I have a structure called SearchAreaListItem. The structure has some properties. The application implements this as a collection.generic.list(of SearchAreaListItem) I load the collection up ...
6
by: MikeSwann | last post by:
Dear All, I am trying to decide on to create a collection object for a project that I am working on. I am fairly new to OOP so this may be on the basic side. I have looked on the groups, but...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
8
by: Andrus | last post by:
Code below causes error in class definition line .....Isolator<T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Myapp.Isolator<T>.GetEnumerator()'...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.