Solved

trying to obtain anonymous object from IList<object>

Posted on 2009-07-03
3
466 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
[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
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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:…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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