LINQ到实体订购by奇怪的问题 [英] linq to entities orderby strange issue

查看:53
本文介绍了LINQ到实体订购by奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是愚蠢的问题,第一次使用linq来访问实体(好吧,通常是linq).

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

具有id(int),value(decimal),name(string)的表

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

我需要的每条记录

id
list<string>
    id
    value
    name

以下工作很好

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:

1100名A

2 300名B

3200个名称C

4100名D

以下使我疯狂的人

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();

带来

1

     1        100     name A

2

     name B   300     2

3

     3        200     name C

4

     name D   100     4

以此类推...

我不明白为什么以及如何在不编写冗长代码的情况下解决问题,我会因爱而跳过.

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

请帮助, Fabrizio

Help please, Fabrizio

推荐答案

我不知道您的代码为什么不起作用,但是尝试使用ToString代替SqlFunction,例如:

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();

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();

更新:我看到您无法让Linq to Entity与您的字符串一起使用.但是您可以在本地进行:)试试:

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();

这篇关于LINQ到实体订购by奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆