+1  A: 

I don't see the point of your extension method as there is already a ToList extension method and that will create a list of your anymous type, what your method will do is create a list, with a single item in it that is an IQueryable of your anonymous type. Secondly the error is saying that GridMaintenanceData has a property called KeyFieldName and you have specified a fieldname in there that does not exist within the datasource you are binding to it, probably because of your silly MakeList method.

Do this instead:

var Qry = from tableRaletions in taskMaints.TaskRelations
    where tableRaletions.TaskId == Convert.ToInt32(txtID.Text) && tableRaletions.RelTypeId == 12
    select new
    {
        tableRaletions.RefMaintenance.code,
        tableRaletions.RefMaintenance.shortdesc
    };
GridMaintenanceData.DataSource = Qry.ToList();
GridMaintenanceData.DataBind();
Ben Robinson
+1  A: 

You've specified a KeyFieldName that does not exist in your DataSource. Exactly what the error says. As far as I can tell from your example, this is nothing to do with the population of your generic list.

From your example, I'm guessing your primary key is RelationId and your KeyFieldName is set to that property. So just change ensure RelationId is return in your select:

select new
{
    tableRaletions.RefMaintenance.RelationId // It's spelt `relations` by the way.
    tableRaletions.RefMaintenance.code,
    tableRaletions.RefMaintenance.shortdesc
};
GenericTypeTea
A: 

Here is your code:

public static List<T> MakeList<T>(T itemOftype)
{
    List<T> newList = new List<T>();
    newList.Add(itemOftype);
    return newList;
}

What don't you use List<T> directly? What's the purpose for MakeList<T>? And

SetCalculatedTaskField.MakeList(Qry)

can be more easy:

Qry.ToList();
Danny Chen
i try to return list value from any class but i must list<myclass> defined...
Phsika
@Danny It is worse than that the MakeList method simply returns a list containing a single item the itemOftype variable passed into it. `SetCalculatedTaskField.MakeList(Qry)` will return a list containing one item with `Qry` in it, it will not return a `Qry` as a list.
Ben Robinson