Solved

How can i fill Anonymous type in list?

Posted on 2010-08-13
5
722 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now