Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

trying to obtain anonymous object from IList<object>

Posted on 2009-07-03
3
Medium Priority
?
470 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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