472,958 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 4717
"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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.