Solved

How can i fill Anonymous type in list?

Posted on 2010-08-13
5
714 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
Comment Utility
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
Comment Utility

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
Comment Utility

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
Comment Utility
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
Comment Utility

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

12 Experts available now in Live!

Get 1:1 Help Now