1

I have a List<object>, and its first element is a List<double>, I mean:

//----

List <object> lista = new List<object>();

List <double> listain = new List<double>();

Lista.Add(listain);


//---

Now I want to obtain the data that is in lista[0] (this element is the List<double>), but I don't know how to do it; I've tried with a foreach, but that doesn't work with objects.

Any idea?


There're some possibilities in different implementations of RDBMS.

For example, in PostgreSQL you can use array or hstore or even JSON (in 9.3 version):

create table Company1 (name text, phones text[]);

insert into Company1
select 'Financial Company', array['111-222-3333', '555-444-7777'] union all
select 'School', array['444-999-2222', '555-222-1111'];

select name, unnest(phones) from Company1;

create table Company2 (name text, phones hstore);

insert into Company2
select 'Financial Company', 'mobile=>555-444-7777, fax=>111-222-3333'::hstore union all
select 'School', 'mobile=>444-999-2222, fax=>555-222-1111'::hstore;

select name, skeys(phones), svals(phones) from Company2    

sql fiddle demo

You can also create indexes on these fields - https://dba.stackexchange.com/questions/45820/how-to-properly-index-hstore-tags-column-to-faster-search-for-keys, Can PostgreSQL index array columns?

In SQL Server, you can use xml datatype to store multivalues:

create table Company (name nvarchar(128), phones xml);

insert into Company
select 'Financial Company', '<phone type="mobile">555-444-7777</phone><phone>111-222-3333</phone>' union all
select 'School', '<phone>444-999-2222</phone><phone type="fax">555-222-1111</phone>'

select
    c.name,
    p.p.value('@type', 'nvarchar(max)') as type,
    p.p.value('.', 'nvarchar(max)') as phone
from Company as c
    outer apply c.phones.nodes('phone') as p(p)

sql fiddle demo

You can also create xml indexes on xml type column.

4

6 回答 6

3

尝试,

foreach (double item in lista [0] as List<double>)
     {

     }
于 2013-09-29T10:33:19.930 回答
2
foreach (var item in lista.Cast<List<double>>())
{

}
于 2013-09-29T10:38:29.610 回答
2

首先,回答您的问题:您必须将第一个元素转换为正确的类

foreach (List<double> doubleList in lista){
    foreach(var number in doubleList){
    // do whatever you want 
    }
}

其次,如果您已经使用了模板,为什么还要将您的List<double>into 和 list 转换为 s?object

尝试像这样定义列表:

List <object> lista = new List<List<double>>();

它不会改变编程,但它使它更易于理解。

于 2013-09-29T10:39:30.207 回答
1

You need to cast that element to your desired datatype. This way.

List<double> col = (List<double>) lista[0];

for (i = 0; i < col.Count; i++)
{
    //Do something
}
于 2013-09-29T10:28:58.173 回答
1

You need to cast it - so:

var doubleList = (List<double>)objectList[0];

Alternatively if you don't know for definite that it will be of that type, you can do

var doubleList = objectList[0] as List<double>;
if (doubleList != null)
{
    // do stuff
}
于 2013-09-29T10:29:09.443 回答
0

如果列表仅包含双打列表,则应使用:

var doubleList = List<List<Double>>();

添加将是这样的:

var double = 1.1;
var insertList = new List<Double>();
insertList.Add(double);
doubleList.Add(insertList);
于 2013-09-29T10:32:51.443 回答