gpt4 book ai didi

.net - LINQ - 动态 orderby 子句不起作用

转载 作者:行者123 更新时间:2023-12-04 18:48:49 24 4
gpt4 key购买 nike

我有这样的代码:

//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 子句,并且记录未排序。

任何的想法?感谢帮助。

最佳答案

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

if (someCondition) 
{
shops = shops.OrderBy(shop => shop.distance);
}
else
{
shops = shops.OrderBy(shop => shop.p.name);
}

关于.net - LINQ - 动态 orderby 子句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9409878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com