how to sort varchar column containing numeric values with linq lambdas to Entity

前端 未结 2 450
长发绾君心
长发绾君心 2021-01-14 06:22

I am using linq lambdas to query the MySql (Note MySql not Sql) with Entity Framwork in MVC. Now i have one table product one of column this table is pric

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 06:52

    It looks right as sorting ascending But when i use OrderByDescending.ThenBy it gives

    106,185,400,34,59,59

    That's because you're ordering by length descending, then value ascending.
    What you need is simply to sort both by descending;

    query = query.OrderByDescending(a => a.price.Length)
                  .ThenByDescending(a => a.price);
    

    This should be faster than prepending numbers to sort, since you don't need to do multiple calculations per row but can instead sort by existing data.

提交回复
热议问题