views:

25

answers:

2

maybe foolish question, first times with linq to entities (well, linq in general).

table with id(int), value(decimal), name(string)

for each record i need

id
list<string>
    id
    value
    name

THE FOLLOWING WORKS FINE

int pageSize=10
int pageIndex=2
var data = (from c in db.Customers
            orderby c.ID
            select new { c.ID, c.Value, c.Name }
             ).Skip(pageSize * pageIndex).Take(pageSize).ToArray();

but doesn'organize the data in the way i need them. However the results are like:

1 100 name A

2 300 name B

3 200 name C

4 100 name D

THE FOLLOWING MAKE ME MAD

int pageSize=10
int pageIndex=2
var data2 = (from c in db.Customers
             orderby c.ID
             select new
             {
                  id = c.ID,
                  cell = new List<string> { 
                     SqlFunctions.StringConvert((double)c.ID), 
                     SqlFunctions.StringConvert(c.Value), 
                     c.Name
                     }
             }
       ).Skip(pageSize * pageIndex).Take(pageSize).ToArray();

which brings

1

     1        100     name A

2

     name B   300     2

3

     3        200     name C

4

     name D   100     4

and so on...

i can't understand why, and how to solve it without writing lenghty code i would skip with love.

Help please, Fabrizio

A: 

I don't know exactly why your code doesn't work, but try using ToString instead of SqlFunction like:

int pageSize = 10;
int pageIndex = 2;
var data = (from c in db.Customers
            orderby c.ID
            select new
            {
                c.ID, 
                cell = new List<string>{ c.ID.ToString(), c.Value.ToString(), c.Name }
            }).Skip(pageSize * pageIndex).Take(pageSize).ToArray();

or

var ordered = db.Customers.OrderBy(c => c.ID);
var page = orderedCustomers.Skip(pageIndex * pageSize).Take(pageSize);

var projection = page.Select(c => new
                 {
                     c.ID,
                     cell = new List<string> { c.ID.ToString(), c.Value.ToString(), c.Name }
                 }).ToArray();

Update: I see you can't get Linq to Entity work with your strings. But you can do it locally then :) try:

var ordered = db.Customers.OrderBy(c => c.ID);
var page = orderedCustomers.Skip(pageIndex * pageSize).Take(pageSize);

var local = page.AsEnumerable();

var projection = local.Select(c => new
                 {
                     c.ID,
                     cell = new List<string> { c.ID.ToString(), c.Value.ToString(), c.Name }
                 }).ToArray();
lasseespeholt
A: 

In the end i followed your second sample, separating linq to entities from linq, and used tostring() Writing all in one sentence i couldn't us to string, entities doesn't support it.

Still i would like to know the logic for the result shuffle i got, is scary and illogic

Tks for your quick answer.

Fabrizio
Yeah, it is weird. I thought Linq to Entity would work with `ToString`. But a solution is simple, I'll update my answer in a couple of minutes. (another time, you should post a comment and not a answer)
lasseespeholt