5

我有这样的代码:

//build query
var shops = (from p in dataContext.shops
let distance = dataContext.GetDistance(p.lat, p.lon, nearlat,nearlon)
                     join c in dataContext.shops_category on p.id equals c.poi_id
                     select new ShopsModel { p = p, distance = distance }
                         );
        }
//add dynamic orderby
if(somthig) 
  shops.OrderBy(distance)
else 
  shops.OrderBy(p.name)


//get records.
return shop.Take(30).ToList()

除了 OrderBy,它工作正常。生成的 SQL 代码不包含 orderby 子句,记录未排序。

任何的想法?感谢帮助。

4

2 回答 2

5

OrderBy 不会改变基础数据 - 它返回一个具有适当排序的可枚举。您需要将结果分配回商店:

if (someCondition) 
{
  shops = shops.OrderBy(shop => shop.distance);
}
else 
{
  shops = shops.OrderBy(shop => shop.p.name);
}
于 2012-02-23T09:08:51.600 回答
1

试试这个:

Shops=shops.OrderBy(distance);
于 2012-02-23T09:06:42.957 回答