我有以下C#LINQ代码:
List<myProductList> qMain = (
from m in db.ProductsList.OrderByDescending
(it => it.GroupCode == 1 || it.L1 == 1)
where m.GoodCode == 1 || m.L1 == 1
select new myProductList
{
StackAmount = m.StackAmount,
GoodCode = m.GoodCode,
PrivateCodeForSort = m.PrivateCodeForSort,
GoodMainCode = m.GoodMainCode,
MaxSellPrice = m.MaxSellPrice,
SellPrice5 = m.SellPrice5,
SellPriceType = m.SellPriceType,
GoodExplain1 = m.GoodExplain1,
Writer = m.Writer,
DragoMan = m.DragoMan,
GoodName = m.GoodName,
Printing = m.Printing,
CoverType = m.CoverType,
PrintYear = m.PrintYear,
PrintPeriod = m.PrintPeriod,
Creation = m.CreationDate
}).Take(50).ToList();
我有一个严重的问题,ProductList表非常大,我不想从我的数据库服务器传输所有行,所以我通过采取50行来解决这个问题,这是我的必要但问题是我想要先通过降序排序,然后取50行,但这段代码首先按升序排50行,然后通过降序排列已经转移和排序的50行来排序。我该如何解决这个问题?
答案 0 :(得分:3)
不,你给的代码:
......按此顺序。
另一方面,这是一个稍微奇怪的OrderByDescending
调用。如果你想要一个不同的排序以及你也可以轻松地做到这一点 - 例如:
from m in db.ProductsList
orderby (it.GroupCode == 1 || it.L1 == 1) descending, it.SellPrice5 descending
where m.GoodCode == 1 || m.L1 == 1
select new myProductList
... // code as before
答案 1 :(得分:0)
这样的事情:
List<myProductList> qMain = (
from m in db.ProductsList.OrderByDescending(it => it.GroupCode == 1 || it.L1 == 1)
where m.GoodCode == 1 || m.L1 == 1
select new myProductList
{
StackAmount = m.StackAmount,
GoodCode = m.GoodCode,
PrivateCodeForSort = m.PrivateCodeForSort,
GoodMainCode = m.GoodMainCode,
MaxSellPrice = m.MaxSellPrice,
SellPrice5 = m.SellPrice5,
SellPriceType = m.SellPriceType,
GoodExplain1 = m.GoodExplain1,
Writer = m.Writer,
DragoMan = m.DragoMan,
GoodName = m.GoodName,
Printing = m.Printing,
CoverType = m.CoverType,
PrintYear = m.PrintYear,
PrintPeriod = m.PrintPeriod,
Creation = m.CreationDate
}).OrderByDescending(m=>m.GoodName).Take(50).ToList();
它不会带来整个数据集;您可以查看生成的SQL查询以进行确认。