?
Solved

trying to obtain anonymous object from IList<object>

Posted on 2009-07-03
3
Medium Priority
?
467 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 2000 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 To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

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)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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