Solved

How can i fill Anonymous type in list?

Posted on 2010-08-13
5
724 Views
Last Modified: 2013-11-11
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....
0
Comment
Question by:programmerist
  • 3
5 Comments
 
LVL 21

Expert Comment

by:tovvenki
ID: 33428408
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
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33428932

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();

0
 
LVL 16

Expert Comment

by:kris_per
ID: 33429258

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

0
 
LVL 10

Expert Comment

by:daryal
ID: 33430153
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);

0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33430663

I think the issue is: var Qry - is IEnumerable list type; you are passing that to MakeList and adding it to another list...so the returned list is actually a list of another list.

Try the below code:



GridMaintenanceData.DataSource = SetCalculatedTaskField.MakeList(Qry.ToList());


public static List<T> MakeList<T>(List<T> list) // change param to List<T>
        {
            List<T> newList = new List<T>();
            newList.AddRange(list);
            return newList;
        }

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question