Solved

How can i fill Anonymous type in list?

Posted on 2010-08-13
5
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

734 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