Problem with e.ContainedCollection.AddRange()

Posts   
 
    
Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 19-Oct-2007 04:09:04   

I have LLBLGen Pro 2.5 using adapter with SqlServer 2k and Asp.Net 2.0

I have two GridViews, both of which are bound to separate LLBLGenProDataSource2 controls. The datasource controls should contain different views of

EntityCollection<OptionContractEntity>

A button click event retrieves the data into the Session, then creates two separate

EntityView2<OptionContractEntity>

Session objects and calls the '.Select()' method of the datasource controls.

I've handled the PerformSelect event on each of the datasource controls and I'm trying to use

e.ContainedCollection.AddRange()

to add the two EntityViews to the datasource controls. I've called the two Views 'Calls' and 'Puts', and I'm using

e.ContainedCollection.AddRange(Calls.ToEntityCollection())

but getting the error 'c isn't of the right type'.

.AddRange() obviously doesn't like the parameter I'm passing to it, even though the objects contained in the parameter implement the IEntity2 interface - as specified in the intellisense.

What am I doing wrong? I've also tried:

IEntityCollection2 col = Puts.ToEntityCollection();
e.ContainedCollection.AddRange(col);

and still get the error.

Is there another way to do this?

All I want to do is retrieve all the data in one hit, and assign the appropriate filtered views to each of the datasource controls.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 19-Oct-2007 12:06:55   

Is the DataSource's EntityFactoryTypeName declared to be set to OptionContractEntityFactoy? Would you please post the ASPX declaration of both datasources?

Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 20-Oct-2007 01:19:06   

Yes, it is set to be of type OptionContractEntityFactory.

Sorry, I can't post the code because I'm on holidays as from last night wink , but it's a general type of question, isn't it?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 21-Oct-2007 11:37:43   
  • Fetch collection (1st collection)
  • create the view for the other collection you want to create on the fetched collection
  • call the ToEntityCollection method on that view (becomes 2nd collection)
  • assign first collection to datasourcecontrol A and the second collection to datasourcecontrol B.

You'll have the same entity instances in both collections. Datasourcecontrols aren't controllers, so they don't offer you to bind views of data OUTSIDE the datasource controls. that's not how the design of datasource controls works (not my design, MS cooked that up). So datasource controls manage the data inside themselves.

Therefore, it might be that an entity which is in both collections gets altered twice, but that's unavoidable due to the design of the datasource controls.

Frans Bouma | Lead developer LLBLGen Pro
Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 22-Oct-2007 01:43:41   

Thanks Otis. The grids are read-only anyway, so it doesn't matter about editing. I'll try what you've suggested.

Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 23-Oct-2007 01:05:00   

Right, tried again.

The problem is, how to assign the two collections to the datasource controls?

In PerformSelect, e.ContainedCollection is read-only, and outside PerformSelect, LLBLGenProDataSource1.EntityCollection is read-only.

It seems the only way is to supply a filter to adapter.GetMulti() inside PerformSelect using e.ContainedCollection as one of the parameters.

I can't see how to assign a collection to the datasource.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 23-Oct-2007 12:10:24   

In PerformSelect, e.ContainedCollection is read-only

I don't think it's readonly, you can fetch it, i.e add entities to it. Therefore it's not readOnly.

It seems the only way is to supply a filter to adapter.GetMulti() inside PerformSelect using e.ContainedCollection as one of the parameters.

I think you mean adapter.FetchEntityCollection();

Please post a simple Repro solution. This will enable us to reproduce the issue as quick as possible.

Also the complete exception text and stack trace please.

tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 09-Jul-2008 21:07:58   

Hello all,

I'm now having this same exact problem. The following is the code that I'm using and I receive the same error. Can someone please help?


void requestItemsDS_PerformSelect(object sender, SD.LLBLGen.Pro.ORMSupportClasses.PerformSelectEventArgs2 e)
{
    EntityCollection<RequestOrderItemEntity> orderItems = new EntityCollection<RequestOrderItemEntity>();
    SD.LLBLGen.Pro.ORMSupportClasses.EntityView2<RequestOrderItemEntity> view = orderItems.DefaultView;
    e.ContainedCollection.AddRange(view.ToEntityCollection());
}

Or


void addNewItem_Click(object sender, EventArgs e)
{
    EntityCollection<RequestOrderItemEntity> orderItems = new EntityCollection<RequestOrderItemEntity>();
    SD.LLBLGen.Pro.ORMSupportClasses.EntityView2<RequestOrderItemEntity> view = orderItems.DefaultView;

    // How do we add this to the grid.
    this.requestItemsDS.EntityCollection.AddRange(view.ToEntityCollection());
}

Both produce the following error.

c isn't of the right type

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 10-Jul-2008 10:47:54   

The problem is caused by the fact that the datasourcecontrol internally creates an EntityCollectionNonGeneric(), which is an EntityCollectionBase2<EntityBase2>. This means that it implements ICollection<EntityBase2> and ICollection<SomeEntityType> isn't castable to ICollection<EntityBase2> because that would require covariance which isn't supported in C# nor VB.NET

AddRange simply loops over the collection passed in and calls Add(), so there are two workarounds for this: 1) use an EntityCollectionNonGeneric and pass that to the AddRange() method in this situation or 2) loop yourself over the entityview and add the entities using Add().

Adding an AddRange() overload which fixes this is tempting, but it could break VB.NET compilers as that compiler easily gets confused with multiple matching overloads.

Frans Bouma | Lead developer LLBLGen Pro