Type parameter conversion error

Oct 22, 2010 at 2:55 PM

I am trying to write a generic method to check to see if an item is in an MList before adding it and also to check and initialize the MList if it has not yet been initialized.

Here is my code:

 

public MList<T> Add2MList<T>(MList<T> list, T item)

{

 

//Get count of items in list.
 

 

int numItems = list.Count<T>();

 

//IF count=0, THEN init list.

 

if (numItems==0)

{

list =

new MList<T>();

}

 

//IF item not in list, THEN add item to list.

 

int matchingItems = Database.Query<T>().Count<T>(i => i.RefName == item.RefName);

 

if (matchingItems == 0)

{

 

//add item to list

list.Add(item);

}

 

//Save list parent object.

 

Database.SaveList<T>(list);

 

return list;

}

This is the error message that I am getting:

IQueryable<T> Database.Query<T>()

Error:

   The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Signum.Engine.Database.Query<T>()'. There is no boxing conversion or type parameter conversion from 'T" to 'Signum.Entities.IdentifiableEntity'.

Has the Query method of Database not been programmed to handle a generic parameter? Do I have to handle this sort of conversion myself?

Perhaps there is something else wrong with my approach? Any help you can provide here will be greatly appreciated. (All my entities use a unique RefName to help me know which instance of a given class is being handled.)

 

Coordinator
Oct 23, 2010 at 4:28 PM

Why you do a database query in order to check if the element is in the MList?

Also, SaveList is not meant to be used for MList, an MList is a relational table. A List is just a collection of entities. 

You can solve the compile problem by adding a constraint to the method: "where T: IdentifiableEntity", but I don't really see it working.

What u trying to accomplish? 

Olmo

Oct 23, 2010 at 4:56 PM

Olmo,

I am trying to accomplish 3 things:

1) initialize the list if it is not already initialized so that I can add an element.

and

2) avoid adding a second copy of the element to the list if it is already in the list.

and

3) do all this in one place instead of repeating the code for each MList that I want to add an element to

 

Coordinator
Oct 23, 2010 at 8:34 PM

MList<T> AddMList<T>(this MList<T> list, T element)
{
if(list == null) list = new MList<T>();

if(!list.Contains(element)) list.Add(element);

retur list; 

 

Unfortunately then u can not do:

customer.Telephones.AddMList("6516215620");

but

customer.Telephones = customer.Telephones.AddMList("6516215620");

At the end is not worth becasse:

* The kind of code that deals with new entities is usually different than the one that deals with retrieved ones.
* For new entities u can use collection initializers or linq queries to populate the mlist
* For retrieved ones u can just add, since the collection is allready initialized

Add only if doesn't exist, however, could be usefull in some situations. Im considering adding to the framework if I found a good name. AddReplace? AddIfNotExist? 

Oct 23, 2010 at 10:58 PM

How about AddIfNew2List?