Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of anonymous types?

I need to create pairs / triples of something and store it somewhere. How can I do it?

I tried:

for (int i = 0; i < 100; i++)
{
    var item=new { a=i , b="lala" ,c=4.5m}; //anonymous type
}

But then I thought: List<what>?

I could use dynamic but I want Intellisense.

(I could have also use Tuple<int,string,decimal> but if I already have what I need (=new { a=i , b="lala" ,c=4.5m};), why should I use other type (tuple)? )

Is there any solution to this?

like image 236
Royi Namir Avatar asked Feb 05 '13 07:02

Royi Namir


2 Answers

You can use type inference

var items = Enumerable.Range(0,100)
                      .Select(i => new { a=i , b="lala", c=4.5m })
                      .ToList(); // not necessary (you can use IEnumerable)
like image 130
Sergey Berezovskiy Avatar answered Sep 28 '22 19:09

Sergey Berezovskiy


Not sure, how you fill fields within for, but could you try:

var lstOfSmth = Enumerable.Range(0, 100)
                            .Select(i => new { a = i, b = "lala", c = 4.5m })
                            .ToList();
like image 42
horgh Avatar answered Sep 28 '22 19:09

horgh