Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Linq to Entity Select Method flip flopping projected lists properties?

I'm have the strangest behavior with linq to entity / Json / MVC.net 4

I have this bit of code, and for some odd reason, every other list's property order is reversed.

var output = db.FooBar.Where(a => a.lookupFoo == bar)
                      .Select(a => new List<double>{
                                     //value's are the same per row 
                                     //for demonstration sake.
                          a.fooBarA,  //Always 12.34
                          a.fooBarB,  //Always 12.34
                          a.fooBarC,  //Always 0
                          a.fooBarD  //Always 0 //lazy casting to double from int
                      });
return Json(new {output});

output looks like so:

{
  "output": [
    [12.34, 12.34, 0,     0], 
    [0,     0,     12.34, 12.34], 
    [12.34, 12.34, 0,     0],
    [0,     0,     12.34, 12.34]
  ]
};

I've managed to work around it by putting a toList() between the Where, and Select, but I'd still like to know why this behavior is happening.

More info: EF 4.4 (tt generated Context), SQL Server 2008r2 express .NET 4.0, MVC 3.0, Vanilla System.Web.Mvc.JsonResult, table consists of a int primary key, floats for values excluding last one which is a int

like image 283
WillFM Avatar asked Feb 25 '13 21:02

WillFM


1 Answers

Try

var output = db.FooBar.Where(a => a.lookupFoo == bar)
                  .Select(a => new List<double>{
                                 //value's are the same per row 
                                 //for demonstration sake.
                      a.fooBarA,  //Always 12.34
                      a.fooBarB,  //Always 12.34
                      a.fooBarC,  //Always 0
                      a.fooBarD  //Always 0 //lazy casting to double from int
                  }).toList();
return Json(output);

On your way output just context the generated script for get data may be it Excute manytime in size Json()

like image 102
Kaiba Zax Avatar answered Oct 23 '22 06:10

Kaiba Zax