Solved

trying to obtain anonymous object from IList<object>

Posted on 2009-07-03
3
457 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

13 Experts available now in Live!

Get 1:1 Help Now