Link to home
Start Free TrialLog in
Avatar of programmerist 1983
programmerist 1983Flag for Türkiye

asked on

How can i fill Anonymous type in list?

I try to write some codes about Generate list from Anonymous type via below codes :
public static List<T> MakeList<T>(T itemOftype)
{
    List<T> newList = new List<T>();
    newList.Add(itemOftype);
    return newList;
}

Open in new window

But ERROR return me:

    A primary key field specified via the KeyFieldName property is not found in the underlying data source. Make sure the field name is spelled correctly. Pay attention to the character case.

Main.cs

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 = SetCalculatedTaskField.MakeList(Qry);
GridMaintenanceData.DataBind();

Open in new window


Please don't say why directly return Tolist(). i know it but i need above....
Avatar of tovvenki
tovvenki

Hi,
Which line is throwing this error?
see if this helps you
http://community.devexpress.com/forums/p/81912/280307.aspx

Thanks and regards,
Venki

var Qry - already has a method named 'ToList()' and you can use that like:

GridMaintenanceData.DataSource = Qry.ToList();

Grid's datasource will work with the list returned by Qry.ToList();


Just saw the last line in your question...then...

it seems the issue is not with MakeList, but in the linq query....from the error message it looks like - taskMaints.TaskRelations doesn't have the key field name....

hello,
maybe i am wrong but,
don't you need to make the call SetCalculatedTaskField.MakeList(Qry) with using a type;
like SetCalculatedTaskField.MakeList<ClassA>(itemTypeOfClassA);

ASKER CERTIFIED SOLUTION
Avatar of kris_per
kris_per

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial