Solved

trying to obtain anonymous object from IList<object>

Posted on 2009-07-03
3
463 Views
Last Modified: 2013-12-17
Hi

I have to use NHibernate and an SQL query (cant use HQL etc.), which means I get a flat table back  not typed objects as usual.

For example:

NHibernateHelper.GetCurrentSession().CreateSQLQuery(query).List<object>();

Where query is some T-SQL code.

This gives me an IList<object> - so the flat table is represented as a list of objects (rows) which are in fact string arrays containing each rows data (columns).

Usually I am dealing with a typed list (e.g. IList<Item> where item is a simple object with a few fields). So I can create anonymous object like this:

IList<Item> list;

object items;

items = from i in list
                        select
                        new
                        {
                            id = i.ItemId,
                            cell = new[]
                                {
                                    i.ItemId.ToString(),
                                    i.ItemName
                                }
                        };

I would like to do the same but do not know how.

Thanks in adavance,

Christian

0
Comment
Question by:csetzkorn
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 24777624
Even though the contained typed is represented as an object (IList) the row data must be of a specific type. Since this type is anonymous you are not able to Cast it so that you LINQ query can refer to the properties when querying the collection.

This dilemna may require you to write a few more lines of code and try getting the properties through reflection.

http://geekswithblogs.net/shahed/archive/2006/11/19/97548.aspx
IList<int> list = null;
List<Item> ItemList = new List<Item>();
 
foreach (var anItem in list)
{
	foreach (PropertyInfo info in anItem.GetType().GetProperties())
	{
		Item newItem = new Item();
 
		if (info.CanRead)
		{
			switch(info.Name.ToLower())
			{
				case "itemid":
					newItem.ItemId = (int)info.GetValue(anItem, null);
					break;
				case "itemname":
					newItem.ItemName = (string)info.GetValue(anItem, null);
					break;
			}
		}
 
		if (newItem.ItemId != default(int) && !String.IsNullOrEmpty(newItem.ItemName))
		{
			ItemList.Add(newItem);
			break;
		}
	}
}

Open in new window

0
 

Author Comment

by:csetzkorn
ID: 24784021
Thanks DTribe,

I did something like this inthe end:

test = new object[list.Count];
                int counter = 0;

                foreach (object[] o in list)
                {
                    test[counter] = new
                    {
                        id = o[0].ToString(),
                        cell = new[]
                        {
                            o[0].ToString(), // ItemId
                            o[1].ToString(), // Version
                            ...
                        }
                    };
                   
                    counter++;
                }
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 24784082
Glad a solution was found
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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