This project is read-only.

Type parameter conversion error

Oct 22, 2010 at 3: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




//Save list parent object.




return list;


This is the error message that I am getting:

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


   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.)


Oct 23, 2010 at 5: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? 


Oct 23, 2010 at 5:56 PM


I am trying to accomplish 3 things:

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


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


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


Oct 23, 2010 at 9: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 = 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 11:58 PM

How about AddIfNew2List?